题解 | #成绩排序#

成绩排序

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

全部评论

相关推荐

Yki_:你要算时间成本呀,研究生两三年,博士三四年,加起来就五六年了,如果你本科去腾讯干五年,多领五年的年薪,加上公司内涨薪,可能到时候十五年总薪资也跟博士差不多
点赞 评论 收藏
分享
后来123321:别着急,我学院本大二,投了1100份,两个面试,其中一个还是我去线下招聘会投的简历,有时候这东西也得看运气
无实习如何秋招上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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