其他分享
首页 > 其他分享> > 成绩排序

成绩排序

作者:互联网

成绩排序

题目描述

在这里插入图片描述

输入

在这里插入图片描述

输出

在这里插入图片描述

样例输入1

5
Alice 99 98 97 96
Bob 98 97 96 94
Coy 94 94 95 96
Dan 93 95 96 97
Evan 0 94 95 95

样例输出1

Alice Bob Coy Dan
Alice Bob Dan Coy
Alice Bob Dan Coy
Dan Alice Coy Evan
Alice Bob Dan Coy

做法1

#include <bits/stdc++.h>

using namespace std;

struct Record {
    string name;
    int c1, c2, c3, c4;
};

int main() {
    int n;
    cin >> n;

    vector<Record> data(n);
    for (int i = 0; i < n; ++i) {
        cin >> data[i].name >> data[i].c1 >> data[i].c2 >> data[i].c3 >> data[i].c4;
    }

    sort(data.begin(), data.end(), [](const Record &lhs, const Record &rhs) {
        return lhs.c1 > rhs.c1 || (lhs.c1 == rhs.c1 && lhs.name < rhs.name);
    });
    cout << data[0].name << " " << data[1].name << " " << data[2].name << " " << data[3].name << endl;

    sort(data.begin(), data.end(), [](const Record &lhs, const Record &rhs) {
        return lhs.c2 > rhs.c2 || (lhs.c2 == rhs.c2 && lhs.name < rhs.name);
    });
    cout << data[0].name << " " << data[1].name << " " << data[2].name << " " << data[3].name << endl;

    sort(data.begin(), data.end(), [](const Record &lhs, const Record &rhs) {
        return lhs.c3 > rhs.c3 || (lhs.c3 == rhs.c3 && lhs.name < rhs.name);
    });
    cout << data[0].name << " " << data[1].name << " " << data[2].name << " " << data[3].name << endl;

    sort(data.begin(), data.end(), [](const Record &lhs, const Record &rhs) {
        return lhs.c4 > rhs.c4 || (lhs.c4 == rhs.c4 && lhs.name < rhs.name);
    });
    cout << data[0].name << " " << data[1].name << " " << data[2].name << " " << data[3].name << endl;

    sort(data.begin(), data.end(), [](const Record &lhs, const Record &rhs) {
        int lt = lhs.c1 + lhs.c2 + lhs.c3 + lhs.c4;
        int rt = rhs.c1 + rhs.c2 + rhs.c3 + rhs.c4;
        return lt > rt || (lt == rt && lhs.name < rhs.name);
    });
    cout << data[0].name << " " << data[1].name << " " << data[2].name << " " << data[3].name << endl;

    return 0;
}

标签:成绩,name,rhs,lhs,c3,排序,data,c4
来源: https://blog.csdn.net/qq_46118239/article/details/117219231