题解 | 小白鼠排队
小白鼠排队
https://www.nowcoder.com/practice/27fbaa6c7b2e419bbf4de8ba60cf372b
#include <algorithm> #include <iostream> #include <vector> #include <string> using namespace std; struct Mouse { int weight; string color; }; bool compare(Mouse lhs, Mouse rhs) { return lhs.weight > rhs.weight; // 从大到小排序 } int main() { int n; cin >> n; // 使用 cin 读取 n vector<Mouse> vec(n); for (int i = 0; i < n; i++) { cin >> vec[i].weight >> vec[i].color; // 使用 cin 读取 weight 和 color } sort(vec.begin(), vec.end(), compare); // 排序 for (int i = 0; i < n; i++) { cout << vec[i].color << endl; // 使用 cout 输出 color } return 0; }