首页 > 试题广场 >

记票统计

[编程题]记票统计
  • 热度指数:124166 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
请实现一个计票统计系统。你会收到很多投票,其中有合法的也有不合法的,请统计每个候选人得票的数量以及不合法的票数。
(注:不合法的投票指的是投票的名字不存在n个候选人的名字中!!)

数据范围:每组输入中候选人数量满足 ,总票数量满足

输入描述:

第一行输入候选人的人数n,第二行输入n个候选人的名字(均为大写字母的字符串),第三行输入投票人的人数,第四行输入投票。



输出描述:

按照输入的顺序,每行输出候选人的名字和得票数量(以" : "隔开,注:英文冒号左右两边都有一个空格!),最后一行输出不合法的票数,格式为"Invalid : "+不合法的票数。

示例1

输入

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

发表于 2021-07-01 01:03:58 回复(0)
while True:
    try:
        c_num = int(input())
        names = list(input().split())
        v_num = int(input())
        votes = list(input().split())
        num_fail = 0
        record = {}
        for na in names:
            record[na] = 0
        for vo in votes:
            if vo in record:
                record[vo] += 1
            else:
                num_fail += 1
        for na in names:
            print(na+' :', record[na])
        print('Invalid :', num_fail)
    except:
        break
发表于 2021-03-28 13:35:49 回复(0)
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

发表于 2021-03-24 14:31:30 回复(0)
while True:
    try:
        n=int(input())
        list1=input().split()
        m=int(input())
        list2=input().split()
        list3=[]
        for i in list1:
            list3.append(list2.count(i))
        y=m-sum(list3)
        list3.append(y)
        list1.append('Invalid')
        for i in range(n+1):
            print('%s : %d'%(list1[i],list3[i]))
    except:
        break

发表于 2021-03-18 14:24:39 回复(0)
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

发表于 2021-03-08 21:38:30 回复(0)
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
发表于 2021-03-04 21:50:44 回复(0)
while True:
    try:
        n = int(input())
        men = input().split()
        m = input()
        votes = input().split()
        valid = 0
        for e in men:
            print("{} : {}".format(e, votes.count(e)))
            valid += votes.count(e)
        print("Invalid : {}".format(len(votes)-valid))
    except:
        break

发表于 2020-12-18 20:32:47 回复(0)
# 思路: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


编辑于 2020-12-14 11:00:22 回复(0)

当测试用例不能全部通过时,还是有原因的。


注意特殊测试用例,
4
A B C D
8
E F G H E F G H
此时都是无效票,字典要使用get()方法,dic[k]会报错。
若使用Counter,不存在的key也会返回0,很完美的避开这个问题。

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


发表于 2020-10-11 13:18:04 回复(0)
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的方法,这题的坑是输出的冒号前后要留空格...醉了醉了
发表于 2020-08-16 22:28:50 回复(0)
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

发表于 2020-07-24 17:29:02 回复(0)
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

为什么通过不了???
编辑于 2020-06-19 23:27:12 回复(0)
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

编辑于 2020-03-30 22:20:48 回复(0)
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

发表于 2020-02-17 23:41:39 回复(0)
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的情况不要忘了,不然根本不知道错哪里
发表于 2019-12-21 18:05:11 回复(2)
# -*- 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

发表于 2019-11-23 22:21:47 回复(0)
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
发表于 2019-09-14 00:43:10 回复(0)
while True:
    try:
        m,dd,n,aa=int(input()),input().split(),int(input()),input().split()
        ss=[]
        sum=0
        for i in dd:
            ss.append([i,aa.count(i)])
        for k in ss:
            sum+=int(k[1])
        for j in ss:
            print(str(j[0])+' : '+str(j[1]))
        print("Invalid : " +str((len(aa)-sum)))
    except:
        break
发表于 2019-08-27 11:10:00 回复(0)
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

题目没说完整,有点坑

发表于 2019-07-03 14:14:30 回复(0)

md,这道题一个坑就是要按照***的顺序输出,一定要注意。。python解法献上

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
发表于 2017-10-04 20:36:23 回复(15)

问题信息

难度:
20条回答 30602浏览

热门推荐

通过挑战的用户

查看代码