[PAT B1020 moon cake]

Chinese people eat moon cake is in the Mid-Autumn Festival a traditional food, different regions have many different flavors of moon cake. Now given all kinds of moon cake inventory, total price, and the maximum demand of the market, you can get the maximum benefit calculated how much.
Note: Allow removing a portion of the stock sale. Case examples given is this: If we have three kinds of moon cake, its stocks were 18,15,10 tons, the total price respectively 75,72,45 billion. If the maximum market demand is only 20 million tons, then we should be the maximum revenue strategy to sell all of the 150,000 tons of the two kinds of moon cake, as well as 50,000 tons of the three kinds of moon cake, get 72 + 45/2 = 94.5 (million) .
Input format:
Each input comprises a test. Each test case is given to a positive integer not more than 1000 N represents the number of types of moon cake, and no more than 500 (in units of thousands of tons) of the positive integer D represents the maximum demand of market. Then given N th row represents a positive number of stocks of each moon cake (in units of thousands of tons); N last line gives positive number indicates a total sales price of each moon cake (in units of billions). Between numbers separated by a space.
Output format:
for each test, the maximum benefit in the output line, in units of billions and accurately to two decimal places.
Sample input:
. 3 20 is
18 is 15 10
75 72 45
Output Sample:
94.50

#include <stdio.h>
#include <algorithm>

using namespace std;
struct cake {
    float price;
    float weight;
    float allSell;
} cake[1010];

bool cmp(struct cake a, struct cake b) {
    return a.price > b.price;
}

int main() {
    float need;
    int kind;
    float sum = 0;
    scanf("%d%f", &kind, &need);
    for (int i = 0; i < kind; i++) {
        scanf("%f", &cake[i].weight);

    }
    for (int i = 0; i < kind; i++) {
        scanf("%f", &cake[i].allSell);
        cake[i].price = cake[i].allSell / cake[i].weight;
    }
    sort(cake, cake + kind, cmp);//按单价从高到低排序
    for (int i = 0; i < kind; i++) {
        if (need < cake[i].weight) {
            sum = sum + need * cake[i].price;
            break;
        } else {
            need = need - cake[i].weight;
            sum = sum + cake[i].allSell;
        }
    }
    printf("%.2f", sum);
    return 0;
}

/*
3 20
18 15 10
75 72 45
 */


Test Results·:
Here Insert Picture Description

Published 35 original articles · won praise 1 · views 4268

Guess you like

Origin blog.csdn.net/qq_39827677/article/details/103955881