题解 | #牛牛的书#

牛牛的书

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;
}

全部评论

相关推荐

点赞 评论 收藏
分享
06-11 17:39
门头沟学院 Java
小呆呆的大鼻涕:卧槽,用户彻底怒了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务