题解 | #成绩排序#

成绩排序

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

stable_sort()排序不改变相同值元素的顺序。

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

bool cmp0(pair<string,int> a, pair<string, int> b)
{ //0为从高到低
        return a.second > b.second; 
}
bool cmp1(pair<string,int> a, pair<string, int> b)
{ //1从低到 高
        return a.second < b.second; 
}

int main()
{
    int n; cin>>n;
    int fs;cin>>fs;
    //建立名字成绩vector
    vector<pair<string,int>> V;
    for(int i = 0; i<n; i++)
    {
        string name; cin>>name;
        int score; cin>>score;
        V.push_back(make_pair(name,score));
    }
    //按成绩排序,0为从高到底
    if(fs == 0)
        stable_sort(V.begin(),V.end(),cmp0);
    if(fs == 1)  //1为从低到高
        stable_sort(V.begin(),V.end(),cmp1);
    //输出排序后的人名和成绩
    vector<pair<string,int>> ::iterator itr;
    for(itr = V.begin(); itr != V.end(); ++itr)
        cout<<(*itr).first<<' '<<(*itr).second<<endl;
    
    return 0;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-02 18:35
简历上把1个月实习写成了3个月,会进行背调吗?
码农索隆:一个月有一个月的实习经历,三个月有三个月的实习经历
简历当中有水分算不算造假...
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-07 18:05
点赞 评论 收藏
分享
05-19 19:57
蚌埠学院 Python
2237:Gpa70不算高,建议只写排名,个人技能不在多而在精,缩到8条以内。项目留一个含金量高的,减少间距弄到一页,硕士简历也就一页,本科不要写很多
实习,投递多份简历没人回...
点赞 评论 收藏
分享
uu们,拒offer时hr很生气怎么办我哭死
爱睡觉的冰箱哥:人家回收你的offer,或者oc后没给你发offer的时候可不会愧疚你,所以你拒了也没必要愧疚他。
点赞 评论 收藏
分享
评论
6
收藏
分享

创作者周榜

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