题解 | #牛牛的书#
牛牛的书
https://www.nowcoder.com/practice/30bb969e117b4f6d934d4b60a2af7489
#include <stdio.h> #include <string.h> typedef struct { char name[100]; int price; } Book; int compare(const void *a, const void *b) { return ((Book *)a)->price - ((Book *)b)->price; } int main() { int n; scanf("%d", &n); Book books[n]; for (int i = 0; i < n; i++) { scanf("%s%d", books[i].name, &books[i].price); } // 使用标准库函数qsort按价格升序排序书籍 qsort(books, n, sizeof(Book), compare); // 输出按价格升序排序后的书名 for (int i = 0; i < n; i++) { printf("%s\n", books[i].name); } return 0; }