题解 | #成绩排序#

成绩排序

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");
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-04 14:23
steelhead:你回的有问题,让人感觉你就是来学习的
点赞 评论 收藏
分享
05-09 13:22
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务