题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/0383714a1bb749499050d2e0610418b1
#include <iostream> #include <algorithm> using namespace std; //相同成绩按先录入排列在前的规则处理。:可以在结构体里给每个用户设置一个int order struct cj{ string name; int score; int order; }; bool cmp1(cj x,cj y) { if(x.score==y.score) return x.order<y.order; else{ return x.score<y.score; } } bool cmp2(cj x,cj y) { if(x.score==y.score) return x.order<y.order; else{ return x.score>y.score; } } int main() { int n,k; while (cin >> n >> k) { cj a[n]; for(int i=0;i<n;i++) { cin>>a[i].name>>a[i].score; a[i].order=i; } if(k==1)sort(a,a+n,cmp1); else{ sort(a,a+n,cmp2); } for(int i=0;i<n;i++) { cout<<a[i].name<<" "<<a[i].score<<endl; } } }