题解 | #记票统计#
记票统计
https://www.nowcoder.com/practice/3350d379a5d44054b219de7af6708894
#include <iostream> using namespace std; #include <map> #include <string> #include <list> int main() { int num1, num2; cin >> num1; map<string, int> hx; string temp; list<map<string, int>::iterator> itlist; for (int i = 0; i < num1; i++) { cin >> temp; hx.insert(pair<string, int>(temp, 0)); itlist.push_back(hx.find(temp)); } hx.insert(pair<string, int>("Invalid", 0)); auto Iv = hx.find("Invalid"); itlist.push_back(Iv); cin >> num2; for (int i = 0; i < num2; i++) { cin >> temp; auto it = hx.find(temp); if (it != hx.end()) { (*it).second++; } else { (*Iv).second++; } } for (auto x : itlist) { cout << (*x).first << " : " << (*x).second << endl; } }
用map进行方便的存储与技术,用个list通过记录迭代器来保存原来的候选人输入顺序以方便输出