HJ68 成绩排序
题面:https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b?tpId=37&tqId=21291&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=
我才知道世界上有一个东西叫做stable_sort,相等时不改变位置的排序。。
1 #include<bits/stdc++.h> 2 using namespace std; 3 struct Stu{ 4 string name; 5 int a; 6 }st[210]; 7 int n,t; 8 bool cmp1(const Stu&a,const Stu&b){ 9 return a.a>b.a; 10 } 11 bool cmp2(const Stu&a,const Stu&b){ 12 return a.a<b.a; 13 } 14 int main(){ 15 cin>>n; 16 cin>>t; 17 for(int i=1;i<=n;i++){ 18 cin>>st[i].name>>st[i].a; 19 } 20 if(t==0)stable_sort(st+1,st+n+1,cmp1); 21 else stable_sort(st+1,st+n+1,cmp2); 22 for(int i=1;i<=n;i++){ 23 cout<<st[i].name<<" "<<st[i].a<<endl; 24 } 25 return 0; 26 }