请实现一个计票统计系统。你会收到很多投票,其中有合法的也有不合法的,请统计每个候选人得票的数量以及不合法的票数。
(注:不合法的投票指的是投票的名字不存在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.
#include <iostream> #include <vector> using namespace std; int main() { int N; while(cin>>N) { vector <string> array(N); int t[65536]={0}; for(int i=0;i<N;i++) { cin>>array[i]; } int voter; cin>>voter; vector <string> array1(voter); for(int j=0;j<voter;j++) { cin>>array1[j]; } for(int i=0;i<N;i++) { for(int j=0;j<voter;j++) { if(array[i]==array1[j]) { t[i]+=1; } } } int sum=0; for(int j=0;j<N;j++) { sum=sum+t[j]; cout<<array[j]<<" "<<":"<<" "<<t[j]<<endl; } cout<<"Invalid"<<" "<<":"<<" "<<voter-sum<<endl; } return 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的情况不要忘了,不然根本不知道错哪里
#include<iostream> #include<string> #include<vector> using namespace std; int main(){ int canNum, votNum; while(cin >> canNum){ vector<pair<string, int>> vpCand; for(int i = 0; i < canNum; ++i){ string canName; cin >> canName; vpCand.push_back(make_pair(canName, 0)); } cin >> votNum; for(int i = 0; i < votNum; ++i){ string votName; cin >> votName; for(int j = 0; j < canNum; ++j){ if(vpCand[j].first == votName) vpCand[j].second ++; } } int valid = 0; for(int i = 0; i < canNum; ++i){ cout << vpCand[i].first << " : " << vpCand[i].second << endl; valid += vpCand[i].second; } cout << "Invalid : " << votNum - valid << endl; } return 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
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.util.HashMap; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; while((line = br.readLine()) != null){ int n = Integer.parseInt(line.trim()); String[] strArr = br.readLine().trim().split(" "); HashMap<String, Integer> counter = new HashMap<>(); for(int i = 0; i < n; i++) counter.put(strArr[i], 0); int countAbnormal = 0; int m = Integer.parseInt(br.readLine().trim()); String[] votes = br.readLine().trim().split(" "); for(int i = 0; i < m; i++){ if(counter.containsKey(votes[i])) counter.put(votes[i], counter.get(votes[i]) + 1); else countAbnormal ++; } for(int i = 0; i < n; i++) System.out.println(strArr[i] + " : " + counter.get(strArr[i])); System.out.println("Invalid : " + countAbnormal); } } }
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
//vector和map结合使用,vector保存***名单,投票个数由map负责存储、输出。程序要考虑到非法/多次输入***情况。 #include<iostream> #include<string> #include<vector> #include<map> using namespace std; int main() { int n; while(cin>>n)//input1 { map<string,int>smap; vector<string>svec; string temp; int num,invalid=0; for(int i=0;i<n;++i) { cin>>temp; svec.push_back(temp); smap[temp]=0; } cin>>num; for(int i=0;i<num;++i) { cin>>temp; if(smap.find(temp)!=smap.end()) ++smap[temp]; else ++invalid; } for(int i=0;i<svec.size();++i) cout<<svec[i]<<" : "<<smap[svec[i]]<<endl; cout<<"Invalid : "<<invalid<<endl; } return 0; }
#include <iostream> #include <string> #include <vector> #include <map> #include <algorithm> using namespace std; int main() { int n; int m; while(cin >> n) { string ss; vector<string> can; vector<string> vote; int i = 0; map<string,int> v; while(n--) { cin>>ss; can.push_back(ss); } cin >> m; while(m--) { cin>>ss; vote.push_back(ss); } int count = 0; for(i = 0; i < vote.size(); i++) { if(find(can.begin(),can.end(),vote[i]) != can.end()) { v[vote[i]]++; count++; //有效票数 } } int invalid = vote.size()-count; for(i = 0; i < can.size(); i++) { cout<<can[i]<< " : "<<v[can[i]]<<endl; } cout<<"Invalid : "<<invalid<<endl; } return 0; }
const res = []; function func(line) { res.push(line); // 如果数组长度等于4,进入计算 if (res.length === 4) { const hNum = res[0]; const hArr = res[1].split(" "); const tNum = res[2]; const tArr = res[3].split(" "); const map = {}; for (let t of tArr) { // 开始统计票数 const key = hArr.includes(t) ? t : "Invalid"; map[key] ? map[key]++ : (map[key] = 1); } // 开始打印结果 for (let h of hArr) { console.log(h + " : " + (map[h] || 0)); } console.log("Invalid : " + (map["Invalid"] || 0)); } } while ((line = readline())) { func(line); }
#include<stdio.h> #include<string.h> int main(){ int n,m; //n候选人数, m投票总数 int record[101]={0}; //某候选人对应的得票数 char vote[20]; //投票字符串 char name[101][20]={'\0'}; //候选人名字字符串 while(~scanf("%d",&n)){ int invalid_vote=0; //无效票数 for(int i=0;i<n;i++) scanf("%s",&name[i]); scanf("%d",&m); for(int i=0;i<m;i++){ scanf("%s",&vote); int j,mark=0; for(j=0;j<n;j++){ // 注意strcmp(str1,str2)字符串比较函数 若两串相等返回的是0 if(!strcmp(name[j],vote)){ mark=1; record[j]++; break; } } if(mark==0) invalid_vote++; } for(int i=0;i<n;i++){ printf("%s : %d\n",name[i],record[i]); } printf("Invalid : %d\n",invalid_vote); } }
numbers = int(input()) name = input().split() T_numbers = int(input()) result = input().split() k = [] l = [] for i in result: if i not in name: k.append(i) else: l.append(i) for j in name: if j in l: x = l.count(j) print(j, ':', x) else: print(j, ':', 0) print('Invalid :', len(k))耗时久了
#include<bits/stdc++.h> using namespace std; int main(){ int n; while(cin>>n){ set<string> myset; map<string,int> mymap; vector<string> flood; int invalid=0; while(n--){ string temp; cin>>temp; myset.insert(temp); flood.push_back(temp); } int votenum; cin>>votenum; while(votenum--){ string vote; cin>>vote; if(myset.find(vote)!=myset.end()){ mymap[vote]++; } else { invalid++; } } for(int i=0;i<flood.size();i++){ cout<<flood[i]<<" : "<<mymap[flood[i]]<<endl; } cout<<"Invalid"<<" : "<<invalid<<endl; } return 0; }
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { // 名字数据初始化 int n1 = sc.nextInt(); String[] names = new String[n1 + 1]; for (int i=0; i<n1; i++) { names[i] = sc.next(); } names[n1] = "Invalid"; // 投票数据初始化 int n2 = sc.nextInt(); String[] votes = new String[n2]; for (int i=0; i<n2; i++) { votes[i] = sc.next(); } // 统计投票并打印 Map<String, Integer> map = count(names, votes); for(String s : names) { System.out.println(s + " : " + map.get(s)); } } } public static Map<String, Integer> count(String[] names, String[] votes) { Map<String, Integer> map = new HashMap<>(); // 初始化 for (String s : names) { map.put(s, 0); } // 统计 for (String s : votes) { if (map.containsKey(s)) { map.put(s, map.get(s) + 1); } else { map.put("Invalid", map.get("Invalid") + 1); } } return map; } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); while(in.hasNextLine()){ int n = Integer.parseInt(in.nextLine()); String a = in.nextLine(); int p = Integer.parseInt(in.nextLine()); String b = in.nextLine(); int count = 0; Map<String,Integer> map = new LinkedHashMap<>(); for(String str:a.split(" ")){ map.put(str,0); } for(String str:b.split(" ")){ if(map.containsKey(str)){ map.put(str,map.get(str)+1); count++; } } for(String key:map.keySet()){ System.out.println(key+" : "+map.get(key)); } System.out.println("Invalid : "+(p-count)); } } }
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <cmath> #include <map> #include <regex> #include <set> using namespace std; int main() { int n; while (cin >> n) { map<string, int> hx; vector<string> order; hx["Invalid"] = 0; for (size_t i = 0; i < n; i++) { string name; cin >> name; hx[name] = 0; order.push_back(name); } order.push_back("Invalid"); int n2; cin >> n2; for (size_t i = 0; i < n2; i++) { string name; cin >> name; if (hx.find(name) != hx.end()) { hx[name]++; } else { hx["Invalid"]++; } } for (int i = 0; i < order.size(); i++) { cout << order[i] << "" << hx[order[i]] << endl; } } return 0; }
#include<iostream> #include<stdio.h> #include<string> #include<algorithm> #include<vector> #include<math.h> #include<unordered_map> using namespace std; int main() { int n; while(cin>>n) { unordered_map<string,int> name; vector<int> res(n,0); vector<string> str(n,""); int id=0; for(int i=0;i<n;++i) { // string s; cin>>str[i]; name[str[i]]=id++; } int ans; cin>>ans; int invalid=0; for(int i=0;i<ans;++i) { string s; cin>>s; if(name.count(s)==0) { ++invalid; continue; } res[name[s]]++; } for(int i=0;i<id;++i) { cout<<str[i]<<" : "<<res[i]<<endl; } cout<<"Invalid : "<<invalid<<endl; } return 0; }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); List<String> list = new ArrayList<>(); for (int i = 0; i < n; i++) { String s = sc.next(); list.add(s); } int m = sc.nextInt(); int invalid = 0; Map<String, Integer> map = new TreeMap<>(); for (int i = 0; i < m; i++) { String s = sc.next(); if (list.contains(s)) { if (map.containsKey(s)) { map.put(s, map.get(s) + 1); } else { map.put(s, 1); } } else { invalid++; } } for (String s : list) { System.out.println(s + " : " + map.getOrDefault(s, 0)); } System.out.println("Invalid : " + invalid); } } }
#include<iostream> #include<string> using namespace std; struct cand //定义一个结构体,参数有姓名、票数 { string name; int num; }; int main() { int n=0,m=0; while (cin>>n) { cand cand[n]; for (int i=0;i<n;i++) //初始化结构体 { cin>>cand[i].name; cand[i].num=0; //注意一开始票数一定要赋0 } cin>>m; int invalid=0; //记录无效票数 for (int i=0;i<m;i++) { string s; int flag=0; cin>>s; for (int j=0;j<n;j++) //找到此人并给他的票数加一 if (s==cand[j].name) {cand[j].num++;flag=1;} if(!flag) invalid++; //若无此人,则为无效票 } for (int i=0;i<n;i++) //最后输出 cout<<cand[i].name<<" : "<<cand[i].num<<endl; cout<<"Invalid : "<<invalid<<endl; } return 0; }
#include <iostream> #include <string> #include <vector> //使用string 数组储存名字 name //对应使用整形数组来储存票数 getpiao //遍历第二次输入的名字,若name中,则getpiao 对应的就++,同时总票数-- using namespace std; int main() { int n; while(cin>>n) { string name[n]; vector<int> getpiao(n,0); for(int i = 0;i<n;i++) cin>>name[i]; int m; cin>>m; int res = m; for(int i = 0;i<m;i++) { string tmp; cin>>tmp; for(int j = 0;j<n;j++) { if(tmp == name[j]) { getpiao[j]++; res --; } } } for(int i = 0;i<n;i++) cout<<name[i]<<" : "<<getpiao[i]<<endl; cout<<"Invalid : "<<res<<endl; } return 0; }
#include<iostream> #include<map> using namespace std; struct person { string name=""; int num=0; }; int main() { int n; while(cin>>n) { person p[n]; string str; int count=0,flag=0,vote; for(int i=0;i<n;i++) { cin>>str; p[i].name=str; } cin>>vote; for(int i=0;i<vote;i++) { cin>>str; for(int j=0;j<n;j++) { if(p[j].name==str) { p[j].num++;flag=1;break; } } if(flag==0) count++; else flag=0; } for(int i=0;i<n;i++) { cout<<p[i].name<<" : "<<p[i].num<<endl; } cout<<"Invalid : "<<count<<endl; } return 0; }