# 【排序】小白鼠再排队

## 题目

N只小白鼠(1 < N < 100)，每只鼠头上戴着一顶有颜色的帽子。现在称出每只白鼠的重量，要求按照白鼠重量从小到大的顺序输出它们头上帽子的颜色。帽子的颜色用 “red”，“blue”等字符串来表示。不同的小白鼠可以戴相同颜色的帽子。白鼠的重量用整数表示。输入输入第一行为一个整数N，表示小白鼠的数目。

```3
30 red
50 blue
40 green```

```red
green
blue```

## 题解

### 1.STL+结构体重载<排序做法：

```#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
using namespace std;
struct Mouse
{
long long weight;
string colorOfHat;
bool operator< (const Mouse& b)
{
return  weight < b.weight;
}
}mouse[120];
int main()
{
int n = 0;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> mouse[i].weight;
cin >> mouse[i].colorOfHat;
}
sort(mouse + 1, mouse + n + 1);
for (int i = 1; i <= n; i++)
{
cout << mouse[i].colorOfHat << endl;
}
}```

### 2.结构体重载+手动冒泡

```#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
using namespace std;
struct Mouse
{
long long weight;
string colorOfHat;
bool operator< (const Mouse& b)
{
return  weight < b.weight;
}
bool operator> (const Mouse& b)
{
return  weight > b.weight;
}
}mouse[120];
int main()
{
int n = 0;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> mouse[i].weight;
cin >> mouse[i].colorOfHat;
}
//sort(mouse + 1, mouse + n + 1);
for (int i = 1; i <= n - 1; i++) {
for (int j = 1; j <= n - i; j++) {
if (mouse[j] > mouse[j + 1]) {
swap(mouse[j], mouse[j + 1]);
}
}
}
for (int i = 1; i <= n; i++)
{
cout << mouse[i].colorOfHat << endl;
}
}```

### 3.pair+冒泡/STL

```#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
using namespace std;
pair<int,string>ans[101];
int main()
{
int n,a;
string s;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
cin>>a>>s;
ans[i]=make_pair(a,s);
}
//sort(ans,ans+n);
for (int i = 1; i <= n - 1; i++) {
for (int j = 1; j <= n - i; j++) {
if (ans[j] > ans[j + 1]) {
swap(ans[j], ans[j + 1]);
}
}
}
for(int i=1;i<=n;i++)
{
cout<<ans[i].second<<endl;
}
return 0;
}
```

1. 说道：
WebView Android 7.0

棒！