PAT 甲級 1070.Mooncake C++/Java

題目來源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.ios

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).c++

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.spa

Sample Input:

3 200
180 150 100
7.5 7.2 4.5

Sample Output:

9.45

題意:

PAT Basic 的1020和這道題同樣code

N:月餅種類數排序

D:月餅的需求three

接下來的兩行輸入:每種月餅的庫存量、每種月餅的總售價ci

分析:

這道題要求的是最大收益,給出月餅的種類和市場需求量,再給出它們的庫存和總售價。get

首先求出不一樣月餅的單價,先計算單價最高的月餅(假定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實現:

相關文章
相關標籤/搜索