小老鼠排队

小白鼠排队

http://www.nowcoder.com/questionTerminal/27fbaa6c7b2e419bbf4de8ba60cf372b

思路

重载一下运算符使得可以从大到小排序就好了

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

struct Rat{
    int weight;
    string color;
    Rat(int w, string c) : weight(w), color(c){}

    bool operator < (const Rat& rat) const {
        return weight > rat.weight;
    }
};

int main(){
    int n;
    while(cin >> n) {
        int weight;
        string color;
        vector<Rat> rats;
        for(int i = 0; i < n; i ++){
           cin >> weight >> color;
            rats.emplace_back(weight, color);
        }
        sort(rats.begin(), rats.end());
        for(Rat r : rats)
            cout << r.color << endl;
    }
    return 0;
} 
算法题解 文章被收录于专栏

不定期更新一些算法题解,有什么问题可以随时留言~

全部评论

相关推荐

点赞 评论 收藏
分享
06-18 13:28
已编辑
门头沟学院 Web前端
爱睡觉的冰箱哥:《给予你300的工资》,阴的没边了
点赞 评论 收藏
分享
仁者伍敌:牛子这些人还会点一个自动回复,boss都不带回复的
点赞 评论 收藏
分享
评论
4
收藏
分享

创作者周榜

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