题解 | #成绩排序#

成绩排序

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

全部评论

相关推荐

kl_我是东山啊:《相关公司:阿里巴巴》
投递阿里巴巴等公司10个岗位
点赞 评论 收藏
分享
2024-12-23 10:55
已编辑
大连理工大学 Java
牛客930504082号:华子综测不好好填会挂的,而且填的时候要偏向牛马选项
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务