PAT 甲级 1070.Mooncake C++/Java

题目来源

Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types of fillings and crusts can be found in traditional mooncakes according to the region's culture. Now given the inventory amounts and the prices of all kinds of the mooncakes, together with the maximum total demand of the market, you are supposed to tell the maximum profit that can be made.

Note: partial inventory storage can be taken. The sample shows the following situation: given three kinds of mooncakes with inventory amounts being 180, 150, and 100 thousand tons, and the prices being 7.5, 7.2, and 4.5 billion yuans. If the market demand can be at most 200 thousand tons, the best we can do is to sell 150 thousand tons of the second kind of mooncake, and 50 thousand tons of the third kind. Hence the total profit is 7.2 + 4.5/2 = 9.45 (billion yuans).

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers N (≤1000), the number of different kinds of mooncakes, and D (≤500 thousand tons), the maximum total demand of the market. Then the second line gives the positive inventory amounts (in thousand tons), and the third line gives the positive prices (in billion yuans) of N kinds of mooncakes. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the maximum profit (in billion yuans) in one line, accurate up to 2 decimal places.

Sample Input:

3 200
180 150 100
7.5 7.2 4.5

Sample Output:

9.45  

题意:

PAT Basic 的1020和这道题一样

N:月饼种类数

D:月饼的需求

接下来的两行输入:每种月饼的库存量、每种月饼的总售价

分析:

这道题要求的是最大收益,给出月饼的种类和市场需求量,再给出它们的库存和总售价。

首先求出不同月饼的单价,先计算单价最高的月饼(假定A的单价最大)

用一个结构体保存月饼的信息:月饼库存,总售价,单价

按照单价高低对月饼信息进行排序,得到单价最高的月饼

情况1:A类月饼总库存 > 市场最大需求量N,则最大收益 = A的单价 * 市场最大需求量

情况2:A类月饼库存 < 市场最大需求量, 则A的收益 = A的单价 * 市场最大需求量

   新的市场最大需求 = 市场最大需求量 减去 A的库存

​ 然后再从单价第二大的计算收益,

最大收益 = 所有月饼收益

注意:库存用double类型,用int的话测试点2过不去

c++实现:

#include <iostream>
#include <algorithm>
#include <string>
#include <cctype>
#include <vector>
#include <stack>
#include <climits>
#include <cstdio>
#include <numeric>
using namespace std;

typedef struct MoonCake {
    double store;    //月饼库存
    double sell;    //总售价
    double unitPrice;    //单价
}MoonCake;

bool myCmp(MoonCake a, MoonCake b)
{
    return a.unitPrice > b.unitPrice;
}

int main()
{
    int N;    //月饼种类数
    double D;    //市场最大需求量
    double result = 0;
    cin >> N >> D;
    vector<MoonCake> vecMoon(N);
    for (int i = 0; i < N; ++i)
    {
        cin >> vecMoon[i].store;
    }
    for (int i = 0; i < N; ++i)
    {
        cin >> vecMoon[i].sell;
        vecMoon[i].unitPrice = vecMoon[i].sell / vecMoon[i].store;
    }
    //按照单价高低排序
    sort(vecMoon.begin(), vecMoon.end(), myCmp);
    for (int i = 0; i < N; ++i)
    {
        if (vecMoon[i].store <= D)
        {    
            //该种类月饼的库存小于市场最大需求
            result = result + vecMoon[i].sell;
        }
        else
        {
            //库存大于市场需求
            result = result + vecMoon[i].unitPrice * D;
            break;
        }
        D = D - vecMoon[i].store;
    }
    printf("%0.2lf", result);
    return 0;
}

Java实现:

posted @ 2020-05-06 09:24  47的菠萝~  阅读(121)  评论(0编辑  收藏  举报