题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/3f27a0a5a59643a8abf0140b9a8cf1f7
#include<cstdio>
#include<algorithm>
using namespace std;
struct Student{
int num;
int grade;
};
bool comp(Student lhs,Student rhs){
//不发生交换的条件,1.左边成绩小于右边 2. 左右成绩相同但是左边学号小于右边
if(lhs.grade < rhs.grade) {
return true;
}
else if (lhs.grade == rhs.grade && lhs.num <= rhs.num){
return true;
}
else {
return false;
}
}
int main(){
Student arr[101];
int n;
scanf("%d",&n);
for(int i = 0; i < n ; ++i){
scanf("%d%d",&arr[i].num,&arr[i].grade);
}
sort(arr,arr+n,comp);
for(int i = 0 ; i < n ; ++i){
printf("%d %d\n",arr[i].num,arr[i].grade);
}
printf("\n");
}
查看16道真题和解析