文章

4

粉丝

22

获赞

6

访问

491

头像
成绩排序 题解:
P1151 清华大学上机题
发布于2025年2月6日 11:05
阅读数 134

通过索引让同分的输出按先后顺序排列

#include <bits/stdc++.h>
using namespace std;

struct Student {
    string name;
    int score;
    int index; // 记录学生输入的顺序
};

// 降序
bool compareDe(const Student &a, const Student &b) {
    if (a.score == b.score) {
        return a.index < b.index; // 分数相同时,按输入顺序排序
    }
    return a.score > b.score;
}

// 升序
bool compareAs(const Student &a, const Student &b) {
    if (a.score == b.score) {
        return a.index < b.index; // 分数相同时,按输入顺序排序
    }
    return a.score < b.score;
}

int main() {
    // num人数,method排序方法
    int num, method;

    while (cin >> num) {
        if (num == 0)
            break;
        cin >> method;

        // ...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发