题解 | #成绩排序#

成绩排序

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

#include<iostream>
#include <iomanip>
#include <algorithm>


using namespace std;

typedef struct student {
    string name;
    int grade;
    int squeue;
} student;

bool compare1(student a, student b) {

    if (a.grade != b.grade) {
        return a.grade < b.grade;
    } else {
        return a.squeue < b.squeue;
    }

}

bool compare2(student a, student b) {

    if (a.grade != b.grade) {
        return a.grade > b.grade;
    } else {
        return a.squeue < b.squeue;
    }
}


int main() {

    int n = 0, way = 0;
    while (::scanf("%d %d", &n, &way) != EOF) {
        student cla[n];
        for (int i = 0; i < n; ++i) {
            cin >> cla[i].name >> cla[i].grade;
            cla[i].squeue = i;
        }
        if (way == 1) {
            sort(cla, cla + n, compare1);
        } else {
            sort(cla, cla + n, compare2);
        }


        for (int i = 0; i < n; ++i) {
            cout << cla[i].name << " " << cla[i].grade << endl;

        }
    }

    return 0;


}

全部评论

相关推荐

07-02 18:09
门头沟学院 Java
苍穹外卖和谷粒商城这俩是不是烂大街了,还能做吗?
想去重庆的鸽子在吐槽:你不如把这俩做完自己搞明白再优化点再来问 何必贩卖焦虑
点赞 评论 收藏
分享
06-10 21:15
门头沟学院 Java
宁阿:好多这种没🧠的公司,他们估计都不知道毕业的人不能给安排实习岗
实习吐槽大会
点赞 评论 收藏
分享
07-02 10:44
门头沟学院 C++
码农索隆:太实诚了,告诉hr,你能实习至少6个月
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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