当前位置: 首页 > 面试经验 >

思科笔试-软开(Golang):比赛

优质
小牛编辑
77浏览
2023-03-28

思科笔试-软开(Golang):比赛

时间没安排好,最后因为边界问题,提交的代码也没通过用例成功。结束五分钟后,才调成功,以下代码仅供参考,还不知道会不会超内存。求Go的工作!!!
package main

import "fmt"

func main() {
	var n, m int
	fmt.Scan(&n, &m)
	temp := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&temp[i])
	}
	flag := 0
	maxIndex := findMax(temp, n)
	// fmt.Println(maxIndex)          // 输出测试
	solve(temp, maxIndex, m, n, flag) // n+1 为A, n+2为B
	for i := 0; i < n; i++ {
		if temp[i] == n+1 {
			fmt.Print("A")
		} else {
			fmt.Print("B")
		}
	}
}

func solve(temp []int, index, m, n, flag int) {
	if index == 111111 {
		return
	}
	count := m
	// fmt.Println(index) // 输出测试
	// fmt.Println(temp) // 输出测试
	tempIndex := index
	for tempIndex >= 0 && count >= 0 { // 这里count不能大于0,要大于等于0。index>=0
		if temp[tempIndex] <= n {
			temp[tempIndex] = n + flag + 1
			tempIndex--
			count--
		} else {
			tempIndex--
		}
	}
	count = m
	tempIndex = index + 1
	for tempIndex < n && count > 0 { // 右边就不能count再多一次了
		if temp[tempIndex] <= n {
			temp[tempIndex] = n + flag + 1
			tempIndex++
			count--
		} else {
			tempIndex++
		}
	}
	if flag == 0 {
		flag = 1
	} else {
		flag = 0
	}
	maxIndex := findMax(temp, n)
	solve(temp, maxIndex, m, n, flag)
}

func findMax(temp []int, n int) int { // 返回数组中最大值所在的位置
	mt := 0
	res := 111111
	for i := 0; i < len(temp); i++ {
		if temp[i] <= n && temp[i] > mt {
			mt = temp[i]
			res = i
		}
	}
	return res
}

测试用例:
输入:
10 2
4 8 9 10 7 6 5 3 2 1
输出:
BAAAAABBBA

输入:
7 1
3 6 1 7 2 5 4
输出:
BBAAABA
#思科#
 类似资料: