小老鼠排队
小白鼠排队
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;
} 算法题解 文章被收录于专栏
不定期更新一些算法题解,有什么问题可以随时留言~
小天才公司福利 1165人发布