//这题要注意的两点是:1.注意要用cin.get()输入其结束标志,要不对getline()的输入造成影响 //2.要注意选举人的票数相同的情况,例如:最高票数的有两个选举人,那么结果就是tie了! #include <iostream> #include <string> #include <algorithm> using namespace std; struct Info { string name, party; int votes; }; bool mycmp(Info a, Info b) { return a.votes > b.votes; } int main() { int i, j, n, m; bool flag = false; string str; Info info[25]; cin >> n; cin.get(); for (i = 0; i < n; i++){ getline(cin, info[i].name); getline(cin, info[i].party); info[i].votes = 0; } cin >> m; cin.get(); for (i = 0; i < m; i++){ getline(cin, str); for (j = 0; j < n; j++){ if (info[j].name == str){ info[j].votes++; break; } } } sort(info, info+n, mycmp); if (info[0].votes == info[1].votes) flag = true; if (flag) cout << "tie" << endl; else cout << info[0].party << endl; system("pause"); } /* 3 Marilyn Manson Rhinoceros Jane Doe Family Coalition John Smith independent 7 John Smith Marilyn Manson Jane Doe John Smith Marilyn Manson John Smith Jane Doe 3 Marilyn Manson Rhinoceros Jane Doe Family Coalition John Smith independent 3 Marilyn Manson Jane Doe John Smith 3 Marilyn Manson Rhinoceros Jane Doe Family Coalition John Smith independent 6 John Smith Marilyn Manson Marilyn Manson Jane Doe John Smith MM */