转跳点:

1020 月饼
 

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

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

输入格式:

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

输出格式:

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

输入样例:

3 20
18 15 10
75 72 45
 

输出样例:

94.50

  这道题基本没有难度,一道简单的贪心,不想思考那么多,用了个结构体,注意月饼给的是总销售金额,不要看成单个零售价,记得先算出单价再排序,qsort不会的赶紧自我反省一下.

  C

 1 #include <stdio.h>
2 #include <stdlib.h>
3 #define MAXSIZE 1024
4
5 typedef struct
6 {
7 double value;
8 double Quantity;
9 double sValue;
10 } Cake;
11
12 int cmp(const void *a, const void *b);
13
14 int main(void)
15 {
16 int CakeKind, Demand;
17 double Profit = 0;
18 Cake cake[MAXSIZE] = {0, 0};
19
20 scanf("%d %d", &CakeKind, &Demand);
21
22 for (int i = 0; i < CakeKind; i++)
23 {
24 scanf("%lf", &cake[i].Quantity);
25 }
26
27 for (int i = 0; i < CakeKind; i++)
28 {
29 scanf("%lf", &cake[i].value);
30 }
31
32 for (int i = 0; i < CakeKind; i++)
33 {
34 cake[i].sValue = cake[i].value / cake[i].Quantity;
35 }
36
37 qsort(cake, MAXSIZE, sizeof(Cake), cmp);
38
39 for (int i = 0; i < CakeKind; i++)
40 {
41 if (0 == Demand)
42 {
43 break;
44 }
45 if (cake[i].Quantity <= Demand)
46 {
47 Profit += cake[i].value;
48 Demand -= cake[i].Quantity;
49 }
50 else
51 {
52 Profit += (Demand / cake[i].Quantity) * cake[i].value;
53 Demand = 0;
54 }
55 }
56
57 printf("%.2lf", Profit);
58
59 return 0;
60 }
61
62 int cmp(const void *a, const void *b)
63 {
64 return (*(Cake *)a).sValue < (*(Cake *)b).sValue ? 1 : -1;
65 }

  C++

 1 #include <iostream>
2 #include <algorithm>
3 #include <iomanip>
4 #include <vector>
5 using namespace std;
6
7 int main()
8 {
9 int n;
10 double D, sum = 0, d;
11 cin >> n >> D;
12
13 vector<double> cake(n), list;
14 for (int i = 0; i < n; i++)
15 {
16 cin >> cake[i];
17 }
18
19 for (int i = 0; i < n; i++)
20 {//填充cake[i]次单价
21 cin >> d;
22 for (int j = 0; j < cake[i]; j++)
23 {
24 list.push_back(d / cake[i]);
25 }
26 }
27
28 sort(list.begin(), list.end(), greater<double>());
29
30 for (int i = 0; i < D && i < list.size(); i++)
31 {//一顿一顿往上加
32 sum += list[i];
33 }
34
35 cout << setiosflags(ios::fixed) << setprecision(2) << sum << endl;
36
37 return 0;
38 }

  PTA不易,诸君共勉!

P 1020 月饼的更多相关文章

  1. PAT乙级 1020. 月饼 (25)(只得到23分)

    1020. 月饼 (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 月饼是中国人在中秋佳节时吃的一种传统食 ...

  2. PAT-乙级-1020. 月饼 (25)

    1020. 月饼 (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 月饼是中国人在中秋佳节时吃的一种传统食 ...

  3. PAT 乙级 1020 月饼 (25) C++版

    1020. 月饼 (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 月饼是中国人在中秋佳节时吃的一种传统食 ...

  4. PAT 1020 月饼 (25)(精简版代码+思路+推荐测试用例)

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

  5. PAT (Basic Level) Practise (中文)-1020. 月饼 (25)

    http://www.patest.cn/contests/pat-b-practise/1020 月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量. ...

  6. PAT(B) 1020 月饼(Java)

    题目链接:1020 月饼 (25 point(s)) 分析 将月饼(库存量,总售价,单价)封装成MoonCake类 Scanner会超时,用BufferedReader类读取数据 读取的时候用字符串数 ...

  7. PAT——1020. 月饼

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

  8. PAT 1020. 月饼 (25)

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

  9. PAT 1020 月饼

    https://pintia.cn/problem-sets/994805260223102976/problems/994805301562163200 月饼是中国人在中秋佳节时吃的一种传统食品,不 ...

随机推荐

  1. 为何以及如何学Linux系统?

    在当今的社会中,linux用处实在是太过广泛了.现在用在服务器和嵌入式上的Linux发行版本数不胜数,桌面上linux只占1%的比例,但这不代表linux比windows和mac 做得差,实际上桌面系 ...

  2. SpringBoot之Feign调用方式比较

    一:事发原因 两个东家都使用SpringCloud,巴拉巴拉用上了Spring全家桶,从eureka到ribbon,从ribbon到feign,从feign到hystrix,然后在使用feign的时候 ...

  3. storm的JavaAPI运行报错

    报错:java.lang.NoClassDefFoundError: org/apache/storm/topology/IRichSpout 原因:idea的bug:本地运行时设置scope为pro ...

  4. vue 项目上传到码云,push时error: failed to push some refs to 'https://gitee.com/mawenrou/vue_ht.git'

    vue 项目上传到码云,push时error: failed to push some refs to 'https://gitee.com/mawenrou/vue_ht.git' 因为之前已经创建 ...

  5. Lognormal distribution 对数正态分布

    转载:https://blog.csdn.net/donggui8650/article/details/101556041 在概率论中,对数正态分布是一种连续概率分布,其随机变量的对数服从正态分布. ...

  6. android EditText中inputType的属性列表

    android 1.5以后添加了软件虚拟键盘的功能,所以在输入提示中将会有对应的软键盘模式 android中inputType属性在EditText输入值时启动的虚拟键盘的风格有着重要的作用.这也大大 ...

  7. [*CTF2019]babyflash

    用JPEXS反编译flash.swf得到441张黑白图片和1个mp3文件 软件下载地址:https://github.com/jindrapetrik/jpexs-decompiler/release ...

  8. Day2-O-Coloring a Tree CodeForces-902B

    You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the ...

  9. Mac 终端启动AVD模拟器

    cd ~/Library/Android/sdk/tools ./emulator -list-avds // 列出av列表 Nexus_5X_API_26 ./emulator @Nexus_5X_ ...

  10. C++中数字与字符串之间的转换 scanf string总结(复习必读)

    1 string的scanf读入操作 C++里面控制台输入直接使用cin操作就可以了:或者getline(istringstream,string); 字符和数字加减就是字符的ASCII码和数字直接加 ...