题解 | #成绩排序#

成绩排序

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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
// 自定义比较函数
struct user{
    string name;
    int score;
};

bool myIncrease(struct user stTmpUser1, struct user stTmpUser2){
    return stTmpUser1.score < stTmpUser2.score;
}

bool myDecrease(struct user stTmpUser1, struct user stTmpUser2){
    return stTmpUser1.score > stTmpUser2.score;
}

int main() {
    int n, bisIncrease;
    cin >> n >> bisIncrease; 
    vector<struct user> userForm;
    struct user stTmpUser;
    for(int i = 0; i < n; i++){
        cin >> stTmpUser.name >> stTmpUser.score;
        userForm.push_back(stTmpUser);
    }

    if(bisIncrease){
        stable_sort(userForm.begin(), userForm.end(), myIncrease);
    }
    else{
        stable_sort(userForm.begin(), userForm.end(), myDecrease );
    }
    
    for(int i = 0; i < n; i++){
        cout << userForm[i].name << " " << userForm[i].score << endl;
    }

    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

09-13 17:25
亲切的00后在笔试:我也遇到了,所以我早他一步查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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