自定义函数—成绩排序
成绩排序
https://www.nowcoder.com/practice/3f27a0a5a59643a8abf0140b9a8cf1f7?tpId=40&&tqId=21340&rp=1&ru=/ta/kaoyan&qru=/ta/kaoyan/question-ranking
自定义函数:当比较函数返回值为true,表示第一个参数会排列在第二个参数前面,反之亦然
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAX=100;
struct Student{
int number;
int score;
};
Student arr[MAX];
bool Compare(Student X, Student Y){
if(X.score == Y.score)
return X.number < Y.number;
else
return X.score<Y.score;
}
int main(){
int n;
scanf("%d", &n);
for(int i=0;i<n;i++)
scanf("%d%d",&arr[i].number,&arr[i].score);
sort(arr, arr+n,Compare);
for(int i=0;i<n;i++)
printf("%d %d\n",arr[i].number,arr[i].score);
return 0;
}