题解 | #成绩排序#

成绩排序

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

#include "cstdio"
#include "algorithm"

using namespace std;

// 结构体二次排序
struct Student {
    unsigned int id;
    unsigned int score;
};

// 结构体二次排序
bool comp4(struct Student stu1, struct Student stu2) {
    if (stu1.score < stu2.score) {
        return true;
    } else if (stu1.score == stu2.score) {
        return stu1.id < stu2.id;	// 结构体二次排序
    } else {
        return false;
    }
}
int main() {
    int n;
    scanf("%d", &n);
    struct Student studs[n + 1];
    for (int i = 0; i < n; ++i) {
        int id, score;
        scanf("%d %d", &studs[i].id, &studs[i].score);
    }
    sort(studs, studs + n, comp4);
    for (int i = 0; i < n; i++) {
        printf("%d %d\n", studs[i].id, studs[i].score);
    }

    return 0;
}

全部评论

相关推荐

我即大橘:耐泡王
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务