题解 | #成绩排序#

成绩排序

https://www.nowcoder.com/practice/0383714a1bb749499050d2e0610418b1

#include <iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef struct Student {
    string name;
    int score;
    int order;//记录先后次序
};
bool Zero(Student a, Student b) {
    if (a.score == b.score) {
        return a.order <
               b.order; //相同成绩都按先录入排列在前的规则处理
    } else {
        return a.score > b.score;
    }
}
bool One(Student a, Student b) {
    if (a.score == b.score) {
        return a.order < b.order;
    } else {
        return a.score < b.score;
    }
}
int main() {
    int n;
    int method;

    while (scanf("%d", &n) != EOF) {
        Student stu[n];
        scanf("%d", &method);
        for (int i = 0; i < n; i++) {
            cin >> stu[i].name >> stu[i].score;
            stu[i].order = i;
        }
        if (method == 0) {
            sort(stu, stu + n, Zero);
        } else if (method == 1) {
            sort(stu, stu + n, One);
        }

        for (int j = 0; j < n; j++) {
            cout << stu[j].name << " " << stu[j].score;
            printf("\n");
        }
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
11-01 08:48
门头沟学院 C++
伤心的候选人在吵架:佬你不要的,能不能拿户口本证明过户给我。。球球了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务