c++一般解法(使用map存储票数,vector<string>存储姓名顺序)
记票统计
http://www.nowcoder.com/questionTerminal/3350d379a5d44054b219de7af6708894
如题,一般解法如下:
ticket存储票数,name存储姓名的顺序,最后输出的时候按照name的顺序输出ticket[name[i]]就行
#include<iostream> #include<vector> #include<string> #include<algorithm> #include<map> using namespace std; int main(){ int num,sum,num2; while(cin >> num){ map<string,int> ticket; vector<string> name; string temp; for(int i = 0; i<num; i++){ cin >> temp; name.push_back(temp); ticket[temp] = 0; } cin >> num2; string vote; int invalid = 0; for(int i = 0; i<num2; i++){ cin >> vote; if(ticket.find(vote) != ticket.end()){ ticket[vote]++; } else invalid++; } for(int i = 0; i<name.size(); i++){ cout << name[i] << " : " << ticket[name[i]] << endl; } cout << "Invalid : " << invalid << endl; //输入一行可用getline } } //return 0; //string.find()若没找到返回-1