题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
#include <iostream> #include <vector> #include <algorithm> using namespace std; bool cmpAce(pair<string, int> a, pair<string, int> b) { return a.second > b.second; } bool cmpDec(pair<string, int> a, pair<string, int> b) { return a.second < b.second; } int main() { int n, flag; string name; int score; vector<pair<string, int>> vscore; pair<string, int> p; cin >> n >> flag; while (n-- > 0) { cin >> name >> score; p.first = name; p.second = score; vscore.push_back(p); } if (flag == 0) { stable_sort(vscore.begin(), vscore.end(), cmpAce); } else if (flag == 1) { stable_sort(vscore.begin(), vscore.end(), cmpDec); } for (auto it : vscore) { cout << it.first << " " << it.second << endl; } return 0; }