小老鼠排队

小白鼠排队

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;
} 
算法题解 文章被收录于专栏

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

全部评论

相关推荐

鬼迹人途:你去投一投尚游游戏,服务器一面,第一个图算法,做完了给你一个策略题,你给出方案他就提出低概率问题,答不上当场给你挂
点赞 评论 收藏
分享
评论
4
收藏
分享

创作者周榜

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