题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
#include <bits/stdc++.h> using namespace std; class bigger{ public: bool operator()(const pair<string, int>& a, const pair<string, int>& b) const{ return a.second > b.second; } }; class smaller{ public: bool operator()(const pair<string, int>& a, const pair<string, int>& b) const{ return a.second < b.second; } }; int main() { int n; cin >> n; int flag; cin >> flag; string name; int score; vector<pair<string, int>> vec; while(n--){ cin >> name; cin >> score; vec.emplace_back(name, score); } if(flag == 0){ std::stable_sort(vec.begin(), vec.end(), bigger()); } else{ std::stable_sort(vec.begin(), vec.end(), smaller()); } for(auto &val : vec){ cout << val.first << " " << val.second << endl; } } // 64 位输出请用 printf("%lld")