PAT Basic 1020. 月饼

PAT Basic 1020. 月饼

1. 题目描述:

月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需求量,请你计算可以获得的最大收益是多少。

注意:销售时允许取出一部分库存。样例给出的情形是这样的:假如我们有 3 种月饼,其库存量分别为 18、15、10 万吨,总售价分别为 75、72、45 亿元。如果市场的最大需求量只有 20 万吨,那么我们最大收益策略应该是卖出全部 15 万吨第 2 种月饼、以及 5 万吨第 3 种月饼,获得 72 + 45/2 = 94.5(亿元)。

2. 输入格式:

每个输入包含一个测试用例。每个测试用例先给出一个不超过 1000 的正整数 \(N\) 表示月饼的种类数、以及不超过 500(以万吨为单位)的正整数 \(D\) 表示市场最大需求量。随后一行给出 \(N\) 个正数表示每种月饼的库存量(以万吨为单位);最后一行给出 N 个正数表示每种月饼的总售价(以亿元为单位)。数字间以空格分隔。

3. 输出格式:

对每组测试用例,在一行中输出最大收益,以亿元为单位并精确到小数点后 2 位。

4. 输入样例:

3 20
18 15 10
75 72 45

5. 输出样例:

94.50

6. 性能要求:

Code Size Limit
16 KB
Time Limit
150 ms
Memory Limit
64 MB

思路:

定义一个结构体存储月饼的相关信息,这里最大收益指的是总售价最多,可能单价越贵的月饼越赚钱吧2333。这里就是按照题目描述将月饼信息读入,然后计算每种月饼的单价,这里我用冒泡排序按照单价降序排序,然后计算总售价,这里一个trick就是当需求量为负数时,表示最后这种月饼卖不完了,需要按照需求量特殊处理下。

另外一个坑点就是最大需求量可能大于所有月饼的库存量总和,这里我多加了一个判断条件才过了testpoint3。好像是参考的大佬的题解(注释的部分):PAT-Basic-1020. 月饼 – Lnyan's Blog (llonely.com)

My Code:

#include <stdio.h>
#include <stdlib.h>
typedef struct
{
    float stock;
    float totalPrice;
    float unitPrice;
} mooncake;

int main(void)
{
    int kindNum = 0, maxNeed = 0;
    mooncake * ptMooncake;
    mooncake temp; // for bubble sort
    unsigned char flag = 0; // for bubble sort
    float res = 0;
    int count = 0; // for calculate result
    
    scanf("%d%d", &kindNum, &maxNeed);
    
    ptMooncake = (mooncake *)malloc(kindNum * sizeof(mooncake));
    
    for(int i=0; i<kindNum; i++)
    {
        scanf("%f", &ptMooncake[i].stock);
    }
    
    for(int i=0; i<kindNum; i++)
    {
        scanf("%f", &ptMooncake[i].totalPrice);
        ptMooncake[i].unitPrice = ptMooncake[i].totalPrice / ptMooncake[i].stock;
    }
    
    for(int i = 0; i<kindNum - 1; i++) // bubble sort
    {
        flag = 0;
        for(int j = kindNum-2; j>=i ; j--)
        {
            if(ptMooncake[j].unitPrice < ptMooncake[j+1].unitPrice)
            {
                temp = ptMooncake[j];
                ptMooncake[j] = ptMooncake[j+1];
                ptMooncake[j+1] = temp;
                flag = 1;
            }
        }
        if(!flag) break;
    }
    
    count = 0;
    res = 0;
    while(maxNeed > 0 && count < kindNum) //here first submit forgot limit count<kindNum, testpoint3 cause the out of bound of arrays
    {
        maxNeed -= ptMooncake[count].stock;
        res = res + ptMooncake[count].totalPrice;
        count++;
    }
    
    if(maxNeed < 0)
    {
        count--;
        maxNeed += ptMooncake[count].stock;
        res -= ptMooncake[count].totalPrice;
        res += ptMooncake[count].unitPrice * maxNeed;
    }
    
    printf("%.2f\n", res);
    
    free(ptMooncake);
    
    return 0;
}



// #include<stdio.h>
// #include<stdlib.h>
// typedef struct mooncake
// {
//     double amount;
//     double sale;
//     double price;
// } MOONCAKE;
// int rcompare(const void *a,const void *b)
// {
//     if(((MOONCAKE *)b)->price>((MOONCAKE *)a)->price)
//         return 1;
//     else if(((MOONCAKE *)b)->price<((MOONCAKE *)a)->price)
//         return -1;
//     return 0;
// }
// /*int rcompare(const void *a,const void *b)
// {
//     if(((MOONCAKE *)b)->sale*((MOONCAKE *)a)->amount>((MOONCAKE *)a)->sale*((MOONCAKE *)b)->amount)
//         return 1;
//     else if(((MOONCAKE *)b)->sale*((MOONCAKE *)a)->amount<((MOONCAKE *)a)->sale*((MOONCAKE *)b)->amount)
//         return -1;
//     return 0;
// }*/
// int main()
// {
//     int i,n,enough=0;
//     double result=0,demand,amount=0;
//     MOONCAKE *mc;
//     scanf("%d%lf",&n,&demand);
//     mc=(MOONCAKE *)malloc(n*sizeof(MOONCAKE));
//     for(i=0;i<n;i++)
//         scanf("%lf",&(mc[i].amount));
//     for(i=0;i<n;i++)
//     {
//         scanf("%lf",&(mc[i].sale));
//         mc[i].price=mc[i].sale/mc[i].amount;
//     }
//     qsort(mc,n,sizeof(MOONCAKE),rcompare);
//     for(i=0;i<n;i++)
//     {
//         amount+=(mc[i].amount);
//         if(amount>demand)
//         {
//             enough=1;
//             break;
//         }
//     }
//     n=i+1;
//     amount=0;
//     for(i=0;i<n;i++)
//     {
//         if(i==n-1&&enough==1)
//         {
//             amount=demand-amount;
//             /*result+=(mc[i].price*amount);*/
//             result+=(mc[i].sale*amount/mc[i].amount);
//             break;
//         }
//         result+=mc[i].sale;
//         amount+=mc[i].amount;
//     }
//     printf("%.2lf\n",result);
//     return 0;
// }
posted @ 2023-03-12 16:12  十豆加日月  阅读(9)  评论(0编辑  收藏  举报