题解 | #牛牛的书#
牛牛的书
https://www.nowcoder.com/practice/30bb969e117b4f6d934d4b60a2af7489
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Book{
char str[512];//书名;
int Price;//价格;
}book_t;
void sort_BookName(book_t* bookType, int n){
int i, j;
char temp_bookname[512];
int temp_price;
for(i = 0; i < n; i++){
for(j = i+1; j < n; j++){
if ((bookType + i)->Price > (bookType + j)->Price) {
strcpy(temp_bookname, (bookType + i)->str);
strcpy((bookType + i)->str, (bookType + j)->str);
strcpy((bookType + j)->str, temp_bookname);
temp_price = (bookType + i)->Price;
(bookType + i)->Price = (bookType + j)->Price;
(bookType + j)->Price = temp_price;
}
}
}
for(i = 0; i < n; i++){
printf("%s\n",(bookType+i)->str);
}
}
int main() {
int p,n,i;
book_t* book;
scanf("%d", &n);
book = (book_t*)malloc(n * (sizeof(book_t)));
for(i = 0; i < n; i++){
scanf("%s %d\n",(book+i)->str, &((book+i)->Price));
}
//for(i = 0; i < n; i++){
//printf("%s\n",(book+i)->str);
//}
sort_BookName(book, n);
return 0;
}

