题解 | #小白鼠排队#
小白鼠排队
https://www.nowcoder.com/practice/27fbaa6c7b2e419bbf4de8ba60cf372b
#include <iostream> #include "algorithm" #include "cstdio" #include "string" using namespace std; typedef struct mouse { int weight; char color[10]; } mouse, *mou; bool compare(mouse lhs, mouse rhs) { if (lhs.weight < rhs.weight) return false; else return true; } int main() { int N; scanf("%d", &N); mouse sample[N]; for (int i = 0; i < N; i++) { scanf("%d %s", &sample[i].weight, &sample[i].color); } sort(sample, sample + N, compare); for (int i = 0; i < N; i++) { printf("%s\n", sample[i].color); } return 0; }
用一个struct存老鼠数据
注意一下调用 sort 时,写一个交换的compare函数 ,如果左<右,就return false,交换,否则不交换.
输出就可以了