述 请实现接口: 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() // 功能:清除投票结果,释放所有资源 // 输入: // 输出:无 // 返回
知识点 查找 运行时间限制 10M 内存限制 128 输入 输入候选人的人数,第二行输入候选人的名字,第三行输入投票人的人数,第四行输入投票。 输出 每行输出候选人的名字和得票数量。 样例输入 4 A B C D 8 A B C D E F G H 样例输出 A : 1 B : 1 C : 1 D : 1 Invalid : 4
#include <unordered_map> #include <string> #include <iostream> using namespace std; unordered_map<string, int> mapname; unsigned int AddCandidate(string pCandidateName) { unordered_map<string, int>::iterator result; result = mapname.find(pCandidateName); if (result == mapname.end()) { mapname.insert(pair<string, int>(pCandidateName, 0)); return 1; } else { return 0; } } void Vote(string pCandidateName,int &count) { unordered_map<string, int>::iterator result; result = mapname.find(pCandidateName); if (result == mapname.end()) { count++; } else { result->second++; } } int main() { int num; cin >> num; int count = 0; for (int i = 0; i < num; i++) { string name; cin >> name; AddCandidate(name); } int people; cin >> people; for (int j = 0; j < people; j++) { string voteName; cin >> voteName; Vote(voteName,count); } unordered_map<string, int>::iterator display; for (display = mapname.begin(); display != mapname.end(); display++) cout << display->first << " : " << display->second << endl; cout << "Invalid : " << count << endl; return 0; } #include <iostream> #include <string> using namespace std; struct vote { string name; int count; }; int main() { int numhx,numvote,Invalid=0; struct vote vt[100]; string s[100]; cin>>numhx; for(int i=0;i<numhx;i++) { cin>>vt[i].name; vt[i].count=0; } cin>>numvote; for(int i=0;i<numvote;i++) cin>>s[i]; for(int i=0;i<numvote;i++) for(int j=0;j<numhx;j++) if(s[i]==vt[j].name) vt[j].count++; for(int i=0;i<numhx;i++) { cout<<vt[i].name<<" : "<<vt[i].count<<endl; Invalid+=vt[i].count; } cout<<"Invalid"<<" : "<<numvote-Invalid<<endl; }