题解 | #成绩排序#

成绩排序

https://www.nowcoder.com/practice/3f27a0a5a59643a8abf0140b9a8cf1f7

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct student {
    int id;
    int score;
} Student;

// int cmp(const void* x, const void* y) {
//     if (((Student*)x)->score == ((Student*)y)->score) {
//         return ((Student*)x)->id > ((Student*)y)->id;
//     } else {
//         return ((Student*)x)->score > ((Student*)y)->score;
//     }
// }

int cmp( Student* x, Student* y) {
    if (x->score == y->score) {
        return x->id > y->id;
    } else {
        return x->score > y->score;
    }
}

int main() {
    Student stu[105];
    int n;
    while (scanf("%d", &n) != EOF) {
        int a[105] = {0};
        for (int i = 0; i < n; i++) {
            scanf("%d %d", &stu[i].id, &stu[i].score);
        }
        qsort(stu, n, sizeof(Student), cmp);
        for (int i = 0; i < n; i++) {
            printf("%d %d\n", stu[i].id, stu[i].score);
        }
    }
    return 0;
}

全部评论

相关推荐

昨天 16:52
已编辑
门头沟学院 Java
周五投的,流程今天结束
投递地平线等公司7个岗位
点赞 评论 收藏
分享
点赞 评论 收藏
分享
鬼迹人途:你去投一投尚游游戏,服务器一面,第一个图算法,做完了给你一个策略题,你给出方案他就提出低概率问题,答不上当场给你挂
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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