题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/3f27a0a5a59643a8abf0140b9a8cf1f7
#include <iostream> #include <algorithm> using namespace std; struct cj{ int id; int score; };//对一组基本类型排序:构造结构体,写cmp函数 cj a[100]; bool cmp(cj x,cj y) { return x.score<y.score; } //更简洁的写法,升级版cmp /* bool cmp(cj x,cj y) { if(x.score==y.score) { return x.id<y.id; } else{ return x.score<y.score; } } */ bool cmpp(cj x,cj y) { return x.id<y.id; } int main() { int n; cin>>n; for(int i=1;i<=n;i++) { cin>>a[i].id>>a[i].score; } sort(a+1,a+n+1,cmp); for(int i=1;i<=n-1;i++) { int flag=1; int k=0; while(flag==1) { if(a[i].score==a[i+1].score){i++;k++;} else { break; } } k++; sort(a+i-k+1,a+i+1,cmpp); } for(int i=1;i<=n;i++) { cout<<a[i].id<<" "<<a[i].score<<endl; } }