题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/0383714a1bb749499050d2e0610418b1
#include <iostream> #include <algorithm> using namespace std; const int N=110; struct node1{ string name; int score; bool operator< (const node1 &t) const{ return score<t.score;//按分数升序排列 } }; struct node0{ string name; int score; bool operator< (const node0 &t) const{ return score>t.score;//按分数降序排列 } }; int main() { int n; while(cin>>n){ int k; cin>>k; if(k==1){ node1 *a=new node1[n]; for(int i=0;i<n;i++){ cin>>a[i].name>>a[i].score; } stable_sort(a,a+n);//satble_sort()是稳定排序 for(int i=0;i<n;i++) cout<<a[i].name<<" "<<a[i].score<<endl; }else{ node0 *b=new node0[n]; for(int i=0;i<n;i++){ cin>>b[i].name>>b[i].score; } stable_sort(b,b+n); for(int i=0;i<n;i++) cout<<b[i].name<<" "<<b[i].score<<endl; } } return 0; } // 64 位输出请用 printf("%lld")