题解 | #成绩排序#

成绩排序

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

#include <iostream>
#include <algorithm>

using namespace std;

struct Student {
    int id;
    int score;
};

Student stu[101];

bool compare (Student x ,Student y){
    if (x.score == y.score){
        return x.id < y.id;
    }
    return x.score < y.score;
}

int main () {
    int n; 
    while (cin >> n){
        for (int i = 0; i < n; i++){
            cin >> stu[i].id >> stu[i].score;
        }
        sort (stu, stu + n, compare);
        for (int i = 0;  i < n; i++){
            cout << stu[i].id << " " << stu[i].score << endl;
        }
    }
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务