题解 | #成绩排序#

成绩排序

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;


}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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