3.14 百度笔试
第一题,买饮料,AC
if __name__ == '__main__': n, k = tuple(map(int, input().split())) fav = list(map(int, input().split())) fav.sort() slow, fast = 0, 0 res = 0 while fast < len(fav): while fav[fast] == fav[slow]: if fast == len(fav)-1: fast += 1 break fast += 1 s = fast - slow res += s // 2 if s % 2 == 0 else s // 2 + 1 slow = fast print(res)第二题,树的叶子节点个数,AC
def dfs(n, k, res): if n <= k&nbs***bsp;(n-k) % 2 == 1: res[0] += 1 return left, right = (n-k) // 2, (n-k) // 2 + k dfs(left, k, res) dfs(right, k, res) if __name__ == '__main__': n, k = tuple(map(int, input().split())) res = [0] dfs(n, k, res) print(res[0])第三题,最大的火柴字数,超时,30%
table = { 1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6, } def dfs(t_l, n, chain, res): if n < 0: return if n == 0: res[0] = max(res[0], int("".join(chain))) return for i in t_l: n -= table[i] chain.append(str(i)) dfs(t_l, n, chain, res) chain.pop() n += table[i] if __name__ == '__main__': res = [0] n, m = tuple(map(int, input().split())) t_l = list(map(int, input().split())) chain = [] dfs(t_l, n, chain, res) print(res[0])