首页 > 试题广场 > 记票统计
[编程题]记票统计

请实现接口:

unsigned int  AddCandidate (char* pCandidateName);
功能:设置候选人姓名
输入: char* pCandidateName 候选人姓名
输出:无
返回:输入值非法返回0,已经添加过返回0 ,添加成功返回1

 

Void Vote(char* pCandidateName);
功能:投票
输入: char* pCandidateName 候选人姓名
输出:无
返回:无


unsigned int  GetVoteResult (char* pCandidateName);

功能:获取候选人的票数。如果传入为空指针,返回无效的票数,同时说明本次投票活动结束,释放资源
输入: char* pCandidateName 候选人姓名。当输入一个空指针时,返回无效的票数

输出:无
返回:该候选人获取的票数

 

void Clear()

// 功能:清除投票结果,释放所有资源
// 输入:
// 输出:无
// 返回

 


输入描述:

输入候选人的人数,第二行输入候选人的名字,第三行输入投票人的人数,第四行输入投票。



输出描述:

每行输出候选人的名字和得票数量。

示例1

输入

4
A B C D
8
A B C D E F G H

输出

A : 1
B : 1
C : 1
D : 1
Invalid : 4

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 回复(5)
//vector和map结合存储,用map查找投票是否合法
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
int main(){
    int m,n;
    while(cin>>m){
        map<string,int> M;
        vector<string> vec(m);
        string s;
        for(int i=0;i<m&&cin>>s;i++){
            M[s]=0;
            vec[i]=s;
        }
        cin>>n;
        for(int i=0;i<n&&cin>>s;i++)
            if(M.find(s)!=M.end())
            	M[s]++;
        int count=0;
        for(auto it=vec.begin();it!=vec.end();it++){
            cout<<*it<<" : "<<M[*it]<<endl;
            count+=M[*it];
        }
        cout<<"Invalid : "<<n-count<<endl;
    }
    return 0;
}

发表于 2016-08-18 14:26:29 回复(2)
#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;
}

发表于 2017-07-12 09:00:05 回复(1)
#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;
}

发表于 2017-04-18 17:18:50 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext()){
            //统计人头
            int n = scan.nextInt();
            LinkedHashMap<String,Integer> map = new LinkedHashMap<String,Integer>();
            scan.nextLine();
            String str = scan.nextLine();
            String[] array = str.split(" ");
            for(int i=0;i<array.length;i++){
                map.put(array[i],0);
            }
            map.put("Invalid",0);
           //投票计数
            int m = scan.nextInt();
            scan.nextLine();
            String strr = scan.nextLine();
            String[] arrays = strr.split(" ");
            for(String s :arrays){
                if(map.containsKey(s)){
                    map.put(s,map.get(s)+1);
                }else{
                     map.put("Invalid",map.get("Invalid")+1);
                }
            }
            Set<String> set = map.keySet();
            for(String s :set){
                System.out.println(s+" : "+map.get(s));
                 
            }
        }
    }
}
冒号左右竟然还有空格,害得我调了半天
发表于 2016-06-17 15:10:12 回复(1)
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();
            Map<String, Integer> map = new LinkedHashMap<String, Integer>();
            for(int i=0; i<n; i++) {
                map.put(sc.next(),0);
            }
            int vote = sc.nextInt();
            int invalid = 0;
            for(int j=0; j<vote; j++) {
                String temp = sc.next();
                if(map.get(temp) == null) {
                    invalid ++;
                } else {
                    map.put(temp,map.get(temp)+1);
                }
            }
            for(Map.Entry entry : map.entrySet()) {
                System.out.println(entry.getKey()+" : "+entry.getValue());
            }
            System.out.println("Invalid : "+invalid);
        }
    }
}

发表于 2018-10-09 14:07:43 回复(0)
//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;
}
编辑于 2017-05-07 20:43:41 回复(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;
}

发表于 2017-03-16 14:13:06 回复(0)
while True:
    try:
        num=int(input())
        ls=list(input().split())
        nn=int(input())
        ls1=list(input().split())
        ll=[]
        for i in ls:
            ll.append(ls1.count(i))
        count=0
        for i in ls1:
            if i not in ls:
                count+=1
        mm=[]
        for i in range(len(ls)):
            s=ls[i]+' : '+str(ll[i])
            mm.append(s)
        mm.append('Invalid : '+str(count))
        for i in mm:
            print(i)
    except:
        break

发表于 2019-08-20 15:53:39 回复(0)
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int findPos(vector<string> ss, string s){
    for (int i = 0; i < ss.size(); i++){
        if (ss[i] == s){
            return i;
        }
    }
    return -1;
}
int main(){
    int n;
    while (cin >> n){
        vector<string> candidate(n,"");
        vector<int>cnt(n, 0);//统计候选人的票数
        int invalid = 0;
        for (int i = 0; i < n; i++){
            cin >> candidate[i];
        }
        int m;
        cin >> m;
        vector<string> voter(m, "");
        for (int i = 0; i < m; i++){
            cin >> voter[i];
            int pos = findPos(candidate, voter[i]);//在候选人中找到被投票人的下标
            if (pos != -1){//找得到则此候选人的票数+1
                cnt[pos]++;
            }
            else{//找不到则invalid+1
                invalid++;
            }
        }
        for (int i = 0; i < candidate.size(); i++){
            cout << candidate[i] << ' ' << ':' << ' ' << cnt[i] << endl;
        }
        cout << "Invalid" << ' ' << ':' << ' ' << invalid << endl;
    }
    return 0;
}

发表于 2019-07-21 19:59:07 回复(0)
两个注意的点
1.候选人名字是String类型
2.用linkedHashMap 保证第一个候选人第一个输出
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();             LinkedHashMap<String, Integer> map=new LinkedHashMap<String, Integer>();             for(int i=0;i<n;i++)             {                 map.put(sc.next(), 0);             }             int Invalid=0;             int m=sc.nextInt();             for(int i=0;i<m;i++)             {    String str=sc.next();                 if(map.containsKey(str))map.put(str, map.get(str)+1);                 else Invalid++;             }               Object[] objects=map.keySet().toArray();               for(int i=0;i<objects.length;i++)               {                   System.out.println(objects[i]+" : "+map.get(objects[i]));               }               System.out.println("Invalid"+" : "+Invalid);         }     }
}

发表于 2019-07-01 21:04:08 回复(0)
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    while(cin >> n)
    {
    vector<string> dna;
    vector<string> cmt;
    string s;
    int res=0;
    for (int i = 0; i < n;i++)
    {
        cin >> s;
        dna.push_back(s);
    }
    vector<int> sum(n, 0);
    int m;
    cin >> m;
    for (int i = 0; i < m;i++)
    {
        cin >> s;
        cmt.push_back(s);
    }
    for (int i = 0; i<n; i++)
    {
        for (int j = 0; j<m; j++)
        {
            if (dna[i] == cmt[j]) sum[i]++;
        }
    
    }
    for (int i = 0; i<n; i++)
    {
        cout << dna[i] << " : " << sum[i] << endl;
        res += sum[i];
    }
    cout << "Invalid" << " : " <<m-res<< endl;
    }
    system("pause");
    return 0;
    
}

发表于 2018-08-05 20:45:15 回复(0)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <map> 
#include <string>
#include <cstring>
#include <vector>
using namespace std;

int main()
{     int n,m;     while(cin>>n)     {         vector<string> s(n);         map<string,int> M;         int invalid = 0;         for(int i=0;i<n;i++)         {             cin>>s[i];             M[s[i]] = 0;         }         cin>>m;         for(int i=0;i<m;i++)         {             string t;             cin>>t;             if(M.find(t)!=M.end())                 M[t]++;             else                 invalid++;         }         for(int i=0;i<n;i++)              cout<<s[i]<<" : "<<M[s[i]]<<endl;                  cout<<"Invalid : "<<invalid<<endl;     }     return 0;
}

发表于 2018-06-18 02:47:45 回复(0)
#include <iostream>  
#include <vector>  
#include <string>
using namespace std;

int main()
{
    int n;
    while (cin>>n)
    {
        vector<string> name;
        for (int i = 0; i < n;++i)
        {
            string tmp;
            cin>>tmp;
            name.push_back(tmp);
        }
        int v;
        cin >> v;
        vector<string> vname;
        for (int i = 0; i < v;++i)
        {
            string tmp;
            cin >> tmp;
            vname.push_back(tmp);
        }
        vector<int> sum;
        for (int i = 0; i < name.size(); ++i)
        {
            int cnt = 0;
            for (int j = 0; j < vname.size(); ++j)
            {
                if (vname[j] == name[i]) cnt++;
            }
            sum.push_back(cnt);
        }
        int Invalid = 0;
        int count = 0;
        for (int i = 0; i < sum.size(); ++i)
            count += sum[i];
        Invalid = vname.size()-count;
        for (int i = 0; i < name.size(); ++i)
        {
            cout << name[i] << " : " << sum[i] << endl;
        }
        cout <<"Invalid : "<< Invalid << endl;
    }
    return 0;
}

发表于 2017-10-02 14:50:41 回复(0)
如果用map,map的底层是set,它量都是无序 的,输出的时候要考虑一下
发表于 2017-07-12 11:05:23 回复(0)
import java.util.Scanner;
import java.util.HashMap;
import java.util.Set;

public class Main{
     public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        while (in.hasNext()){
            int n = in.nextInt();
            String[] s = new String[n];
            HashMap<String,Integer> hs = new HashMap<String,Integer>();
            for(int i=0;i<n;i++){
                String temp = in.next();
                hs.put(temp,0);
                s[i] = temp;
            }
            int m = in.nextInt();
            Set<String> keys = hs.keySet();
		   int cnt =  0;
		   for(int i=0;i<m;i++){
			   String tick = in.next();
			   if(keys.contains(tick)){
				   hs.put(tick, hs.get(tick)+1);
			   }else{
				   cnt++;
			   }
		   }
		   for(String k:s){
			   System.out.println(k+" : "+hs.get(k));
		   }
		   System.out.println("Invalid : "+cnt);
        }
        in.close();
     }
}

发表于 2017-07-10 10:36:42 回复(1)
#include<iostream>
#include<unordered_map>
#include<string>
#include<vector>
using namespace std;
int main() {//值的注意的是unordered_map并不保证按照输入顺序输出
	int nums;
	while (cin >> nums) {
		unordered_map<string, int> hash;
		vector<string> names;
		string tmp;
		int invalid = 0;
		while (nums--) {
			cin >> tmp;
			if (hash.find(tmp) == hash.end()) hash[tmp] = 0;
			names.push_back(tmp);
		}
		cin >> nums;
		while (nums--) {
			cin >> tmp;
			if (hash.find(tmp) == hash.end()) ++invalid;
			else ++hash[tmp];
		}
		for (auto i : names) cout << i << " : " << hash[i] << endl;
		cout << "Invalid : " << invalid << endl;
	}
}
发表于 2017-03-20 18:28:20 回复(0)
import java.util.LinkedHashMap;
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 N=sc.nextInt();
			int invalid=0;
			Map<String, Integer> map=new LinkedHashMap<>();
			for(int i=0;i<N;i++){
				map.put(sc.next(), 0);
			}
			int M=sc.nextInt();
			for (int i = 0; i < M; i++) {
				String s=sc.next();
				if (map.containsKey(s)) {
					map.put(s, map.get(s)+1);
				}else {
					invalid++;
				}
			}
			for(String key:map.keySet()){
				System.out.println(key+" : "+map.get(key));
			}
			System.out.println("Invalid : "+invalid);
		}
	}

}
比较简单,用到了Map。。。。。没多的好说

发表于 2016-06-28 17:12:12 回复(0)
# include<stdio.h>
# include<string.h>
struct Candidate
{
    char name[100];
    int vote;
};
int main()
{
    struct Candidate s[100];
    int i,j,n,votes;
    while(scanf("%d",&n)!=EOF)
    {
        int invalid=0;
        char c[100];
        int flag=0;
        for(i=0;i<n;i++)
        {
            scanf("%s",s[i].name);
            s[i].vote=0;
        }
        scanf("%d",&votes);
        for(i=0;i<votes;i++)
        {
            scanf("%s",c);
            flag=0;
            for(j=0;j<n;j++)
            {
                if(!strcmp(c,s[j].name)) //字符比较可以用==,字符串,C语言中要用函数咯!!!
                {
                    s[j].vote++;
                    flag=1;
                }
            }
            if(flag==0) invalid++;
        }
       for(i=0;i<n;i++)
           printf("%s : %d\n",s[i].name,s[i].vote);
        printf("Invalid : %d\n",invalid);
            
    }
    return 0;
}
发表于 2016-05-25 11:52:36 回复(0)
import java.util.Scanner;

public class Main{
public static int[] vote(String[] str1,String[] str2){//统计投票
int[] in=new int[str1.length];
for (int i = 0; i < str1.length; i++) {
for (int j = 0; j < str2.length; j++) {
if (str2[j].equals(str1[i])) {
in[i]++;
}
}
}
return in;
}
public static int other(int[] in){//计算有多少有效票
int temp=0;
for (int i = 0; i < in.length; i++) {
temp+=in[i];
}
return temp;
}
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
while(scanner.hasNext()){
int num1=scanner.nextInt();//输入候选人数量
String[] string1=new String[num1];
for (int i = 0; i <num1; i++) {
string1[i]=scanner.next();//输入候选人姓名
}
int num2=scanner.nextInt();//输入投票人数量
String[] string2=new String[num2];
for (int i = 0; i < num2; i++) {
string2[i]=scanner.next();
}
int[] in=vote(string1, string2);
for (int i = 0; i < in.length; i++) {
System.out.print(string1[i]+" : "+in[i]);
            System.out.println();
}
System.out.println("Invalid : "+(num2-other(in)));
}
}
}
发表于 2016-04-09 19:33:32 回复(2)