文章

81

粉丝

2

获赞

493

访问

9.5k

头像
成绩排序 题解:
P1151 清华大学上机题
发布于2026年3月18日 12:07
阅读数 357

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool cmp_up(pair<string,int> a, pair<string,int> b){
    return a.second < b.second;
}

bool cmp_down(pair<string,int> a, pair<string,int> b){
    return a.second > b.second;
}

int main(){
    int n, x;

    while(cin >> n >> x){
        vector<pair<string,int> > v;

        for(int i = 0; i < n; i++){
            string name;
            int grade;
            cin >> name >> grade;
            v.push_back(make_pair(name, grade));
        }

        if(x == 0)
            stable_sort(v.begin(), v.end(), cmp_down);
        else
            stable_sort(v.begin(), v.end(), cmp_up);

        for(int i = 0; i < n; i++){
            cout << v[i].first << " " << v[i].second << endl;
        }
    }

    return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发