题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/0383714a1bb749499050d2e0610418b1
#include <iostream> #include<cstdio> #include<algorithm> using namespace std; struct student{ string name; int score; int order; }; student arr[10000]; int Comparede(student a,student b){ if(a.score==b.score) return a.order<b.order; else return a.score>b.score; } int Compareas(student a,student b){ if(a.score==b.score) return a.order<b.order; else return a.score<b.score; } int main() { int n,t; while(scanf("%d%d",&n,&t)!=EOF){ for(int i=0;i<n;i++){ cin>>arr[i].name>>arr[i].score; arr[i].order=i;} if(t==0) sort(arr,arr+n,Comparede); else sort(arr,arr+n,Compareas); for(int i=0;i<n;i++) cout<<arr[i].name<<" "<<arr[i].score<<endl; } return 0; } // 64 位输出请用 printf("%lld")