vivo,秋招算法笔试
第一题 去括号
AC 100%
def solution(s): # TODO Write your code here s = s.replace('()', '') s = s.replace('0', '') cnt = 0 while '()' in s: s = s.replace('()', '') cnt += 1 return cnt if __name__ == '__main__': s = input() print(solution(s))
第二题 服务器
没运行,不知道对不对
def solution(total_disk, total_memory, app_list): # TODO Write your code here size = len(app_list) if size == 0: return 0 dp = [[[0 for _ in range(total_memory + 1)] for _ in range(total_disk + 1)] for _ in range(size)] for i in range(total_disk+1): for j in range(total_memory+1): if app_list[0][0] <= i and app_list[0][1] <= j: dp[0][i][j] = app_list[0][2] for k in range(1, size): for i in range(total_disk + 1): for j in range(total_memory + 1): if app_list[0][0] <= i and app_list[0][1] <= j: dp[k][i][j] = max(dp[k][i][j], app_list[0][2]+dp[k-1][i-app_list[0][0]][j-app_list[0][1]]) return dp[size-1][total_disk][total_memory] if __name__== "__main__": input1 = [x for x in input().strip().split(' ')] total_disk = int(input1[0]) total_memory = int(input1[1]) app_list = [[int(i) for i in x.strip().split(',')] for x in input1[2].strip().split('#')] print(solution(total_disk, total_memory, app_list))
第三题:消消乐
AC 50% 知道自己的投机取巧的办法
def solution(boxes): # TODO Write your code here cnt = [0] * 10 for i in boxes: cnt[i] += 1 res = [x**2 for x in cnt] return sum(res) if __name__ == '__main__': x = input() boxes = list(map(int,x.split())) print(solution(boxes))