请实现一个计票统计系统。你会收到很多投票,其中有合法的也有不合法的,请统计每个候选人得票的数量以及不合法的票数。
(注:不合法的投票指的是投票的名字不存在n个候选人的名字中!!)
数据范围:每组输入中候选人数量满足 ,总票数量满足
第一行输入候选人的人数n,第二行输入n个候选人的名字(均为大写字母的字符串),第三行输入投票人的人数,第四行输入投票。
按照输入的顺序,每行输出候选人的名字和得票数量(以" : "隔开,注:英文冒号左右两边都有一个空格!),最后一行输出不合法的票数,格式为"Invalid : "+不合法的票数。
4 A B C D 8 A D E CF A GG A B
A : 3 B : 1 C : 0 D : 1 Invalid : 3
E CF GG三张票是无效的,所以Invalid的数量是3.
while True: try: n=int(input())#记录没用的玩意儿 people = input().split()#记录候选人,以空格区分 v = int(input())#记录总票数 volt = input().split()#记录投票给谁,以空格区分 a=[] for i in range (len(people)): a.append(volt.count(people[i]))#用于记录有效票数,查找每个有效候选人的总票数 str1 =people[i]+" : "+str(volt.count(people[i]))#按顺序按格式打印 print(str1) b=list(map(int,a))#元素转换为int格式 b=sum(b)#求全部有效票数总和 invalid = v - b#全部票数-总有效票数=无效票数 print('Invalid : '+str(invalid))#打印无效票数 except: break
while True: try: num = int(input()) zeros = [0 for _ in range(num)] order = input().split(' ') candidates = dict(zip(order, zeros)) vote_num = int(input()) votes = input().split(' ') invalid = 0 for i in range(vote_num): if votes[i] in candidates: candidates[votes[i]] += 1 else: invalid += 1 for i in range(num): print(order[i], ':', candidates[order[i]]) print('Invalid :', invalid) except EOFError: break
while True: try: n=int(input()) name=input().split() m=int(input()) vote=input().split() res=[] num=0 for i in name: res.append(str(vote.count(i))) num+=vote.count(i) for i in range(n): print(name[i],':',res[i]) #print(res) print('Invalid',':',str(len(vote)-num)) except: break
while True: try: H_nums = int(input()) H = input().split() V_nums = int(input()) V = input().split() dic = {} for p in H: dic[p] = 0 nums = 0 for val in V: if val in H: dic[val]+=1 nums+=1 for key in dic: print(key,':',dic[key]) print('Invalid',':',len(V)-nums) except: break
# 思路:1)任意错的地方是直接把候选人固定成例题给的ABCD四个人,要注意人数和票数是 # 随着不同的输入而改变的。需要迭代取出。 # 2)本题创建字典的时候不能用以往的方法:如果存在于字典中,次数加1,若不存在,次数为1. # 本题的每个候选人必须存在于字典中,故需提前将每个候选人都加入字典中。 # 3)注意输出的格式:冒号两侧有一个空格 while True: try: human_num = int(input().strip()) human_name = input().strip().split(' ') ticket_num = int(input().strip()) ticket = input().strip().split(' ') # 构建候选人以及票数的字典项。并将每个候选人的票数初始化为0,手动添加非法的票数项 dic = {} for human in human_name: dic[human] = 0 dic['Invalid'] = 0 for t in ticket: if t not in human_name: dic['Invalid'] += 1 else: dic[t] += 1 # 输出结果(包括候选人的票数和非法票数) for human in human_name: print(human+' : '+str(dic[human])) print('Invalid'+' : '+str(dic['Invalid'])) except: break
while 1: try: c,c_name,v,v_ticket = int(input()),input().split(),int(input()),input().split() dic = {} # 统计票数 for k in v_ticket: dic[k] = dic.get(k,0)+1 # dic = Counter(v_ticket) # 输出,按候选人顺序格式 valid = 0 for i in c_name: print("%s : %s"%(i,dic.get(i,0))) valid += dic.get(i,0) print("Invalid : %s"%(len(v_ticket)-valid)) except: break
while True: try: n = int(input()) name = input().split() m = int(input()) s = input().split() error = m for i in name: print("%s : %d"%(i,s.count(i))) error -= s.count(i) print('Invalid : %d'%(error)) except: break给一个python的方法,这题的坑是输出的冒号前后要留空格...醉了醉了
def get_result(rm, tp): dd = dict() for i in rm: dd[i] = 0 dd['Invalid'] = 0 for j in tp: if j in dd: dd[j] += 1 else: dd['Invalid'] += 1 for m, n in dd.items(): print('%s : %s' % (m, n)) while True: try: rs = int(input()) rm = input().split() ps = int(input()) tp = input().split() get_result(rm, tp) except: break
while True: try: num=int(input()) name=[i for i in input().split()] v_num=int(input()) vote=[i for i in input().split()] vote_dic=dict.fromkeys(name,0) for i in vote: for j in name: if i==j: vote_dic[i]+=1 for (i,j) in vote_dic.items(): print("{0}:{1}".format(i,j)) print("Invalid:",v_num-sum(vote_dic.values())) except: break
while True: try: n = int(input()) candidate = input().split() m = int(input()) voters = input().split() for i in range(len(candidate)): count = [] for j in range(len(voters)): if candidate[i] == voters[j]: count.append(candidate[i]) print(candidate[i]+' '+':'+' '+str(len(count))) invalid = 0 for i in voters: if i not in candidate: invalid += 1 print('Invalid'+' '+':'+' '+str(invalid)) except: break
while True: try: n1,a,n2,b,count = int(input()),input().split(),int(input()),input().split(),0 for i in a: if i in b: count += b.count(i) print(str(i) + " : " + str(b.count(i))) else: print(str(i) + " : " + str(b.count(i))) print("Invalid : " + str(n2-count)) except: break
while True: try: houn = input() hou_l = input().split() piaon= int(input()) piao_l = input().split() youxiao =0 for i in hou_l: if i in piao_l: print(i +" : "+str(piao_l.count(i))) youxiao += piao_l.count(i) else: print(i +" : "+str(piao_l.count(i))) print('Invalid : '+str(piaon-youxiao)) except: break无票输出0的情况不要忘了,不然根本不知道错哪里
# -*- coding: utf-8 -*- # !/usr/bin/python3 # 解题思路:利用字典保存合法次数,不合法的单独用invalid记录 while True: try: m = int(input()) candidates = input().split() n = int(input()) votes = input().split() dic = {} invalid = 0 for i in candidates: dic[i] = 0 for i in votes: if i in candidates: dic[i] += 1 else: invalid += 1 for i in candidates: print('{0} : {1}'.format(i, dic[i])) print('Invalid : {0}'.format(invalid)) except: break
while True: try: n = int(input()) valid_names = list(map(str, input().split())) total = int(input()) vote = list(map(str, input().split())) vote_info = dict(); invalid = 0 for elem in vote: if elem in valid_names: if elem not in vote_info.keys(): vote_info[elem] = 1 else: vote_info[elem]+=1 else: invalid+=1 for key in valid_names: if key in vote_info.keys(): print(key+' : '+str(vote_info[key])) else: print(key + ' : ' + str(0)) print("Invalid : "+str(invalid)) except: break
while True: try: n=int(input().strip()) name=list(input().strip().split(' ')) #print(name) num=int(input().strip()) piao=list(input().strip().split(' ')) #print(piao) dict1={} worng=0 for i in piao: if i in name: if i not in dict1: dict1[i]=1 else: dict1[i]+=1 else: worng+=1 #print(dict1) for i in name: if i in dict1: print(i+' : '+str(dict1[i])) else: print(i+' : '+str(0)) print('Invalid :',worng) except: break
题目没说完整,有点坑
from collections import Counter
while True:
try:
a, b, c, d, invalid = input(), input().split(), input(), input().split(), 0
cc = Counter(d)
for i in b:
print(i + " : " + str(cc[i]))
invalid+=cc[i]
print("Invalid : " + str(len(d)-invalid))
except:
break