成绩排序
程序员文章站
2024-03-21 19:47:10
...
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
/*简单题;
struct 和 sort 的结合;
cmp函数
*/
typedef long long LL;
const int N=1e5+10,mod=1e6;
struct student{
string name ;
int order ;
int grade ;
} ;
bool cmp1(const student &a , const student &b){
if(a.grade == b.grade){
return a.order < b.order ;
}else {
return a.grade > b.grade ;
}
}
bool cmp2(const student &a , const student &b){
if(a.grade == b.grade){
return a.order < b.order ;
}else {
return a.grade < b.grade ;
}
}
int main(){
int n ;
int m ;
while(cin >> n >> m){
student a[10000] ;
for(int i = 1 ; i <= n ; i++ ){
cin >> a[i].name >> a[i].grade ;
a[i].order = i ;
}
if(m == 0){
sort(a+1 , a+n+1 , cmp1) ;
}else if(m == 1){
sort(a+1 , a+n+1 , cmp2) ;
}
for(int i = 1 ; i <=n ; i ++){
cout<<a[i].name<<' '<<a[i].grade <<endl;
}
}
}