华为4.21笔试 A了两题,分享下代码
Python代码,攒点人品,有想法评论留言哈~
第一题:
采用数组来做,两层循环, 正确率100%
import sys n, m = input().split() anpai = [0 for i in range(24)] for i in range(int(n)): A, L, C = input().split() C = int(C) flag = 0 # 记录该时间端是否满员 for j in range(int(A), int(L)): if(anpai[j] + C > int(m)): flag = 1 break if flag == 0: for j in range(int(A), int(L)): anpai[j] += C for i in range(24): print(anpai[i], end=' ')
第二题:
对于无效模块,采用数组,判断是否出现过来数个数
对于循环模块,采用递归,不断试路线来解决
正确率88%,应该是没有考虑分数为负值的情况,可惜了
import sys m = int(input()) n = int(input()) num = {} have = [] for i in range(n): son, fat = input().split() #----将出现的加入进去 if son not in have: have.append(son) if fat not in have: have.append(fat) # --------- if son in num: num[son].append(fat) else: num[son] = [fat] NoUseNum = m - len(have) CircleNum = 0 for son in num: # 对每个数字的依赖进行循环,并且不断的跳入循环,直到走到只有一个数组或发现循环 stack = [son] # 走入他的依赖,就将他放入栈 stackTimes = [len(num[son])] # 维护一个依赖列表的数量,确保将所有依赖都走一遍 break2 = False while sum(stackTimes) != 0: if stackTimes[-1]-1 >= 0: path = num[stack[-1]][stackTimes[-1]-1] if path in stack: break2 = True CircleNum = 1 break stack.append(path) if path not in num: stackTimes.append(0) else: stackTimes.append(len(num[path])) else: stackTimes.pop() stack.pop() stackTimes[-1] -= 1 if break2: break print(int(10 - NoUseNum - 2 * CircleNum))