题解 | #成绩排序#

成绩排序

https://www.nowcoder.com/practice/0383714a1bb749499050d2e0610418b1

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

struct sc{
    string name;
    int score;
};

bool cmp1(sc a,sc b){
    return a.score<b.score;
}

bool cmp2(sc a,sc b){
    return a.score>b.score;
}

int main() {
    int a, b;
    while (cin >> a >> b) { // 注意 while 处理多个 case
        vector<sc> v;
        string s;int t;
        while(a--)
        {
            cin>>s>>t;
            v.push_back({s,t});
        }
        if(b)
            stable_sort(v.begin(),v.end(),cmp1);
        else
            stable_sort(v.begin(),v.end(),cmp2);
        for(auto i:v)
        {
            cout<<i.name<<" "<<i.score<<endl;
        }
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

东孝子_强东我偶像:你怎么当孝子都和我时间一样😭
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务