其实这道题一点都不难,只不过我对c++的语法不熟悉,很是收益很多的,比如string,pair,输入等等
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int cmp(pair<string, int> p1, pair<string, int> p2) {
return p1.second > p2.second;
}
int main() {
int n, m;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> m;
cin.get();
pair<string, int> data[101];
for (int j = 0; j < m; j++) {
string s,s1;
int year;
getline(cin, s);
int len = s.size();
s1 = s.substr(0, s.size() - 5);
year = atoi(s.substr(s.size() - 4, s.size()).c_str());
data[j] = make_pair(s1, year);
}
sort(data, data+m, cmp);
for (int i = 0; i < m; i++) {
cout << data[i].first << endl;
}
}
return 0;
}