文章

19

粉丝

0

获赞

125

访问

3.0k

头像
成绩排序 题解:
P1151 清华大学上机题
发布于2025年3月10日 07:44
阅读数 39

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

struct node {
    string name;
    int score;
};

bool cmp_desc(node a, node b) {
    return a.score > b.score; 
}

bool cmp_asc(node a, node b) {
    return a.score < b.score; 
}

int main() {
    int n, ch;
    while (cin >> n >> ch) { 
        vector<node> a(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i].name >> a[i].score;
        }

        if (ch == 0) {
            stable_sort(a.begin(), a.end(), cmp_desc);
        } else {
            stable_sort(a.begin(), a.end(), cmp_asc); 
        }

        for (auto item : a) {
            cout << item.name << " " << item.score << endl;
        }
    }
    return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发