题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
#include <algorithm> #include <functional> #include <iostream> #include <utility> #include <vector> using namespace std; int main() { int n; while (cin >> n) { int low_to_high; cin>>low_to_high; vector<pair<int, string>> content(n); for(int i=0;i<n;i++){ int score; string name; cin>>name>>score; content[i] = {score, name}; }// 不能使用greater<>和less<> first相等的时候会比较second if(low_to_high) stable_sort(content.begin(), content.end(), []( const pair<int, string>& a, const pair<int, string>& b){ return a.first<b.first; }); else{ stable_sort(content.begin(), content.end(), []( const pair<int, string>& a, const pair<int, string>& b){ return a.first>b.first; }); } for(int i=0;i<n;i++){ cout<<content[i].second<<' '<<content[i].first<<endl; } } }