题解 | #成绩排序#

成绩排序

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

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

class MyCompare1 { //stable_sort的仿生函数
  public:
    bool operator()(pair<string, int> p1,
                    pair<string, int>
                    p2) { //函数对象根据stable_sort函数的对象来看,每一个比较的是一个pair<string,int>类型的值,参数为这。
        return (p1.second) > (p2.second);
    }
};
class MyCompare2 {
  public:
    bool operator()(pair<string, int>p1, pair<string, int>p2) {
        return (p1.second) < (p2.second);
    }
};

int main() {
    int n;
    cin >> n;
    int re;
    cin >> re;
    vector<pair<string, int>>p(n);
    for (int i = 0; i < n; i++) {
        cin >> p[i].first >> p[i].second;
    }
    if (re == 0) {
        stable_sort(p.begin(), p.end(),
                    MyCompare1()); //稳定排序,遇到相同数值时,可以保留原来元素的位置顺序。
    } else {
        stable_sort(p.begin(), p.end(), MyCompare2());
    }
    for (int i = 0; i < n; i++) {
        cout << p[i].first << " " << p[i].second << endl;
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

星辰再现:裁员给校招生腾地方
点赞 评论 收藏
分享
身边有人上海、深圳&nbsp;6、7k&nbsp;都去了,真就带薪上班了。
程序员小白条:木的办法, 以后越来越差,还是家附近宅着吧,毕业的人越来越多,岗位都提供不出来,经济又过了人口红利期
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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