输入描述:
多案例输入,每个案例的输入第一行为一个整数N,表示小白鼠的数目。
下面有N行,每行是一只白鼠的信息。第一个为不大于100的正整数,表示白鼠的重量,;第二个为字符串,表示白鼠的帽子颜色,字符串长度不超过10个字符。
注意:白鼠的重量各不相同。
输出描述:
每个案例按照白鼠的重量从大到小的顺序输出白鼠的帽子颜色。
示例1
输入
复制
3
30 red
50 blue
40 green
输出
复制
blue
green
red
经验总结
逆序迭代器rbegin()、rend()
map<int, string> mp;
map<int,string>::reverse_iterator it;//没有这句话不能使用逆序迭代器
- 只有deque、vector、string是可以使用SORT的,而MAP不允许,可以使用sort进行结构体排序;
bool cmp(mouse a,mouse b){
return a.weight>b.weight;
}
sort(s, s + n, cmp);
AC代码1
//使用MAP和逆序来算的
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
int main(){
int t;
map<int, string> mp;
map<int,string>::reverse_iterator it;
while (cin >> t)
{
for(int i = 0;i < t;i++){
int j = 0;
cin >> j >> mp[j];
}
for(auto it = mp.rbegin();it != mp.rend();it++){
cout << it->second<<endl;
}
}
return 0;
}
AC代码2
//利用结构体进行计算
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
struct Mouse{
int weight;
string color;
}s[100];
bool cmp(Mouse a, Mouse b){
return a.weight > b.weight;
}
int main(){
int n;
while(cin>>n){
for(int i = 0; i < n; i++){
cin>>s[i].weight>>s[i].color;
}
sort(s, s + n, cmp);
for(int i = 0; i < n; i++){
cout<<s[i].color<<endl;
}
}
return 0;
}