文章

61

粉丝

98

获赞

17

访问

17.7k

头像
成绩排序2.0 题解:sort函数实现
P1159 清华大学上机题
发布于2024年1月23日 14:37
阅读数 402

#include <bits/stdc++.h>
using namespace std ;

typedef struct student{   //定以结构体
    int id ;   //学号
    int score ;   //成绩
} Student ;

bool cmp(Student a ,Student b){
    if(a.score == b.score)  return a.id < b.id;   //成绩相等取学号小的
    else return a.score < b.score ;
}

int main( ){
    int n ;
    cin >> n ;
    Student stu[n] ;
    for(int i = 0 ;i < n ;i ++)
        scanf("%d %d" ,&stu[i].id ,&stu[i].score) ;
    stable_sort(stu ,stu+n ,cmp) ;
    for(int i = 0 ;i < n ;i ++)
        printf("%d %d\n" ,stu[i].id ,stu[i].score) ;
    return 0 ;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发