10221 - moocHW7a   

Description

搜尋某個產品名稱
找出符合的產品
並且依照評價高低
從評價高分到低分排序
如果評價相同
則繼續依照價格由低價到高價排序

範例程式

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

// #define ONLINE_JUDGE

/*
struct for product items
*/
typedef struct _Product {
    char name[51];
    float price;
    float review;
    int  num_reviews;
} Product;

int compare(const void *a, const void *b)
{
    Product *ia, *ib;
    ia = *(Product **)a;
    ib = *(Product **)b;
    /* your code here */
}

void show_product(Product *item)
{
    printf("%s, ", item->name);
    printf("$%.2f, ", item->price);
    printf("%.1f\n", item->review);
}

int main(void)
{
    Product **items;
    int i, j;
    int ndata, nqueries;
    char query[51];

#ifndef ONLINE_JUDGE
    freopen("testcase1", "r", stdin);
    //freopen("out1", "w", stdout);
#endif
    scanf("%d", &ndata);
    while (getchar() !='\n');

    items = (Product**) malloc(sizeof(Product*) * ndata);

    for (i=0; i<ndata; i++) {
        items[i] = (Product*) malloc(sizeof(Product));
        fgets(items[i]->name, 31, stdin);
        items[i]->name[strlen(items[i]->name)-1] = 0;
        scanf("%f", &items[i]->price);
        scanf("%f", &items[i]->review);
        scanf("%d", &items[i]->num_reviews);
        while (getchar() !='\n');
    }

    scanf("%d", &nqueries);
    while (getchar() !='\n');

    qsort(items, ndata, sizeof(Product *), compare);

    for (i=0; i<nqueries; i++) {
        /* your code */
    }

    for (i=0; i<ndata; i++) {
        free(items[i]);
    }
    free(items);
    return 0;
}

 

Input

第一行的數字代表有幾個產品
接著每四行代表一個產品的資訊:
產品名稱
產品價格
產品評價
產品評價數

再來是一個數字,代表有幾筆 query
之後的每一行代表一筆 query
每筆 query 的內容為品牌名稱

Output

根據每筆 query 輸出:
品牌名稱
產品名稱1, 產品價格1, 產品評價1
...
產品名稱N, 產品價格N, 產品評價N

每一個品牌名稱下面可能會接好幾個產品
請依照題目要求將這些產品排序好

Sample Input  Download

Sample Output  Download

Tags

sort



Discuss