题解 | #小白鼠排队# 王道 机试指南 第二章 排序
小白鼠排队
https://www.nowcoder.com/practice/27fbaa6c7b2e419bbf4de8ba60cf372b
#include<algorithm> #include<iostream> #include<string.h> using namespace std; struct Mouse { int weight; char color[10]; }; bool compp(Mouse x, Mouse y) { return x.weight > y.weight; //按照白鼠的重量从大到小的顺序输出白鼠的帽子颜 } int main() { Mouse mouse[101]; int n, i; while (scanf("%d", &n) != EOF) { for (i = 0; i < n; i++) { cin >> mouse[i].weight >> mouse[i].color; } sort(mouse, mouse + n, compp); //按照白鼠的重量从大到小的顺序输出白鼠的帽子颜 //此时,数据中时=是已经排列好的鼠鼠,现在要输出最肥的鼠的帽子颜色 for (i = 0; i < n; i++) { cout << mouse[i].color << endl; } } return 0; }