题解 | #成绩排序#

成绩排序

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

#include<bits/stdc++.h>
using namespace std;
int flag;
struct student {
    string name;
    int score;
};
bool mycmp(student a, student b) {

    if (flag==0) {
        if (a.score >b.score) return 1;
        else return 0;
    } else {
        if (a.score >=b.score) return 0;
        else return 1;
    }
}
bool cmp(student a,student b){
    if(flag==0) return a.score>b.score;
    else return a.score<b.score; //这里必须写else,否则牛客会编译失败
}
int main() {
    int n;
    while (cin>>n) {
         cin >> flag;
        student stu[n];
        for (int i = 0; i < n; i++)
            cin >> stu[i].name >> stu[i].score;
        stable_sort(&stu[0], &stu[n], mycmp);
        for (int i = 0; i < n; i++) cout << stu[i].name << ' ' << stu[i].score << endl;
    }
    return 0;
}

全部评论

相关推荐

08-21 16:35
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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