题解 | #记票统计#
记票统计
https://www.nowcoder.com/practice/3350d379a5d44054b219de7af6708894
#分析:候选人是键,加一个无效键,投票数量是值,无效票也是值,考差了字典的基本用法,本使用了dict.get(key,default = 0) 重要内置函数 while 1: try: n,names,m,votes,d = int(input()),input().split(),int(input()),input().split(),{} for i in votes: if i in names: d[i] = d.get(i,0) + 1 #如果票有效,则加+1 else: d["Invalid"] = d.get("Invalid",0) + 1 for i in names: print(i,":",d.get(i,0)) print("Invalid",":",d.get("Invalid",0)) except: break