题解 | 成绩排序

成绩排序

https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b

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

struct Student {
    string name;
    int score;
};

bool OrderDown(Student stu1, Student stu2) {
    if (stu1.score < stu2.score) {
        return false;
    } else if (stu1.score == stu2.score) {
        return false;
    }
    return true;
}

bool OrderUp(Student stu1, Student stu2) {
    if (stu1.score > stu2.score) {
        return false;
    } else if (stu1.score == stu2.score) {
        return false;
    }
    return true;
}
int main() {
    int n;
    scanf("%d", &n);
    int op;
    scanf("%d", &op);
    vector<Student> stu_vc;
    for (int i = 0; i < n; i++) {
        Student stu;
        char c[1024];
        scanf("%s%d", c, &stu.score);
        stu.name = c;
        stu_vc.push_back(stu);
    }
    if (op) { //1、按成绩升序
        stable_sort(stu_vc.begin(), stu_vc.end(), OrderUp);
    } else { //0、按成绩降序
        stable_sort(stu_vc.begin(), stu_vc.end(), OrderDown);
    }
    for(int i=0;i<n;i++){
        printf("%s %d\n",stu_vc[i].name.c_str(),stu_vc[i].score);
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

04-03 15:12
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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