题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/0383714a1bb749499050d2e0610418b1
//注意有多组数据;用cin cout方便字符串的读取和输入; //两个比较函数用在sort里 //了解更多sort函数访问http://www.cplusplus.com/reference/algorithm/sort/ #include <iostream> #include <algorithm> #include <cstdio> using namespace std; struct Student{ string name; int score; int order; }; bool Comparejiang(Student x,Student y){ if(x.score==y.score){ return x.order<y.order; } else{ return x.score>y.score; } } bool Comparesheng(Student x,Student y){ if(x.score==y.score){ return x.order<y.order; } else{ return x.score<y.score; } } int main() { int n,type; while(scanf("%d%d",&n,&type)!=EOF){ Student arr[n]; for(int i=0;i<n;++i){ cin>>arr[i].name>>arr[i].score; arr[i].order=i; } if(type==0){ sort(arr,arr+n,Comparejiang); } if(type==1){ sort(arr,arr+n,Comparesheng); } for(int i=0;i<n;i++) cout<<arr[i].name<<" "<<arr[i].score<<endl; } return 0; }