题解 | #成绩排序#

成绩排序

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

#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
struct P{
    string name;
    int score;
} stu[N];
bool cmp_up(P p1, P p2){
    return p1.score < p2.score;
}
bool cmp_down(P p1, P p2){
    return p1.score > p2.score;
}

int main() {
    int n;
    int flag;
    while(cin >> n){
        cin >> flag;
        for(int i = 0; i < n; i ++){
            cin >> stu[i].name >> stu[i].score;
        }
        //重点:sort是不稳定排序,stable_sort才是稳定排序
        if(flag == 0) stable_sort(stu, stu + n, cmp_down);
        else stable_sort(stu, stu + n, cmp_up);
        for(int i = 0; i < n; i ++)
            cout << stu[i].name <<" "<<stu[i].score << endl;
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

07-02 10:44
门头沟学院 C++
码农索隆:太实诚了,告诉hr,你能实习至少6个月
点赞 评论 收藏
分享
06-20 19:40
中原工学院 Java
网络存储:十几天不会让你拉人办卡就结束了吧?
点赞 评论 收藏
分享
程序员小白条:你是沟通了900个,不是投了900份简历,你能投900份,意味着对面都要回复你900次,你早就找到实习了,没亮点就是这样的,别局限地区,时间投的也要早,现在都要7月了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务