输入用户名和成绩,将成绩按照给出的0或1,判断由高到低排序或由低到高排序,相同成绩都按先录入者排序在前的规则处理
input/output:
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct Student{
string name;
int score;
};
bool comp1(Student a, Student b){
if(a.score == b.score)
return &a < &b;
else
return a.score<b.score;
}
bool comp0(Student a, Student b){
if(a.score == b.score)
return &a < &b;
else
return a.score > b.score;
}
int main(){
int count;
scanf("%d", &count);
Student a[count];
int x;
scanf("%d", &x);
for(int i=0; i<count; i++)
cin>>a[i].name>>a[i].score;
if(x == 0)
sort(a, a+count, comp0);
else
sort(a, a+count, comp1);
cout<<endl;
for(int j=0; j<count; j++)
cout<<a[j].name<<" "<<a[j].score<<endl;
}