pat甲级 10700---Mooncake

来源:互联网 发布:折800淘宝 编辑:程序博客网 时间:2024/06/15 10:10

精度坑
1.题目,我把会出错的地方标出来了啊
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(2位小数).
2.题目解析
2.1求出最大盈利数。并且每种月饼的库存数量可以拆分卖,不是背包问题。因此需要按照单价进行排序即可。
2.2 注意题目中给出的各种输入的范围,其中月饼种类和市场需求量是正整数int,而月饼的库存量是正数也就是类型必须是double,月饼的价钱是正数double。
3.题目实现
3.1利用数组或者结构体都可以,按照单价进行排序。
3.2利用市场需求量进行循环求出最后的盈利。
3.3代码如下

#include <iostream>#include <stdio.h>#include <algorithm>using namespace std;struct mooncake{    double unit_price,price;    double amount;};bool compare1(mooncake cake1,mooncake cake2){        return cake1.unit_price > cake2.unit_price;}int main(){    int n;    double demand;    scanf("%d %lf",&n,&demand);    mooncake mycake[n];    for(int i=0;i<n;i++){        double amount;        scanf("%lf",&amount);        mycake[i].amount = amount;    }    for(int i=0;i<n;i++){        double price;        scanf("%lf",&price);        mycake[i].price = price;        mycake[i].unit_price = price/mycake[i].amount;    }    sort(mycake,mycake+n,compare1);    double sum = 0;    int i = 0;    while(demand!=0&&i<n){        if(mycake[i].amount>0){                if(demand>=mycake[i].amount){                    sum += mycake[i].price;                    demand = demand-mycake[i].amount;                    mycake[i].amount = 0;                }                else{                    sum += demand/mycake[i].amount*mycake[i].price;                    mycake[i].amount -= demand;                    demand = 0;                }        }        else{            i++;        }    }    printf("%.2lf\n",sum);    return 0;}

4**注意事项**
月饼的库存必须为double,否则真的会有一个案例过不去!