题目描述:
解题思路:
代码实现:
#include <iostream>
#include <algorithm>
using namespace std;
struct Candidate {
string name;
int cnt;
};
const int N = 100;
Candidate arr[N];
int main() {
int n, m;
cout << "please input the number of n and m:" << endl;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cout << "please input candidate's name:";
cin >> arr[i].name;
arr[i].cnt = 0;
}
while (m--) {
string name;
cout << "input the name of candidate:" << endl;
cin >> name;
for (int j = 0; j < n; j++) {
if (arr[j].name == name) {
arr[j].cnt++;
}
}
}
sort(arr, arr + n, [](const Candidate& a, const Candidate & b) {
return a.cnt > b.cnt;
});
for_each(arr, arr + n, [](const Candidate& x) {
cout << x.name << " : " << x.cnt << endl;
});
return 0;
}