B站 开发笔试 Golang
打卡题 AK
class Solution: def DoubleNumber(self, n): length = len(str(n)) cur = 10 res = 0 while 1: temp = str(cur) if len(temp) * 2 > length: break temp = str(cur) + str(cur) if 1 < int(temp) and int(temp) < n: res += 1 if int(temp) > n: break return res
func main() { scanner := bufio.NewScanner(os.Stdin) n := 0 nums := make([]int, 0) if scanner.Scan() { temp := strings.Split(scanner.Text(), " ") n, _ = strconv.Atoi(temp[0]) } if scanner.Scan() { temp := strings.Split(scanner.Text(), " ") for _, v := range temp { temp, _ := strconv.Atoi(v) nums = append(nums, temp) } } n++//没用 flag := 100000000 res := 10000000000 var dfs func(target []int, left, right int) dfs = func(target []int, left, right int) { if len(target) == 0 { if abs(left-right) < flag { flag = abs(left - right) res = max(left, right) } return } dfs(target[1:], left+target[0], right) dfs(target[1:], left, right+target[0]) } dfs(nums, 0, 0) fmt.Println(res) } func max(a, b int) int { if a > b { return a } return b } func abs(a int) int { if a > 0 { return a } return -a }