题解 | 牛牛的书

牛牛的书

https://www.nowcoder.com/practice/30bb969e117b4f6d934d4b60a2af7489

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// write your code here......
struct Books{
    char** BooksName;
    int* BooksPrice;
}books;
void swap_int(int* a, int* b){
    int temp = *a;
    *a = *b;
    *b = temp;
}
void swap_char(char** a, char** b){
    char* temp = *a;
    *a = *b;
    *b = temp;
}
void up_out(char** book_name, int* book_price, int len){
    struct Books books = {book_name, book_price};
    for(int i=0; i<len-1; i++){
        for(int j=0; j<len-i-1; j++){
            if(book_price[j]>book_price[j+1]){
                swap_int(&book_price[j], &book_price[j+1]);
                swap_char(&book_name[j], &book_name[j+1]);
            }
        }
    }
    for(int i=0; i<len; i++){
        printf("%s\n", books.BooksName[i]);
    }
}
int main() {

    int n;
    scanf("%d",&n);

    char** book_name=(char**)malloc(n*sizeof(char*));
    int* book_price=(int*)malloc(n*sizeof(int));

    for (int i = 0; i < n; i++) {
        book_name[i]=(char*)malloc(100*sizeof(char));
        scanf("%s %d",book_name[i],&book_price[i]);
    }

    // write your code here......
    up_out(book_name, book_price, n);
    for (int i = 0; i < n; i++) {
        free(book_name[i]);
    }
    free(book_name);
    free(book_price);
    return 0;
}

C/C++题解 文章被收录于专栏

记录个人编程题的解题思路以及学习的新知识

全部评论

相关推荐

03-06 18:20
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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