当前位置:网站首页>PTA 天梯赛练习题集 L2-003 月饼 测试点2,测试点3分析
PTA 天梯赛练习题集 L2-003 月饼 测试点2,测试点3分析
2022-07-07 00:27:00 【qascetic】
月饼
月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需求量,请你计算可以获得的最大收益是多少。
注意:销售时允许取出一部分库存。样例给出的情形是这样的:假如我们有 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
思路:用结构体保存月饼的库存量、总售价和单价。本题中月饼单价越高收益越多。结构体数组的每个元素都是一种月饼。所以用月饼单价排序结构体数组,把单价高的月饼放前面。从单价高的月饼开始填补市场,这样总收益高。
注意:每个测试用例先给出一个不超过 1000 的正整数 N 表示月饼的种类数、以及不超过 500(以万吨为单位)的正整数 D 表示市场最大需求量。随后一行给出 N 个正数 表示每种月饼的库存量(以万吨为单位);最后一行给出 N 个正数表 示每种月饼的总售价(以亿元为单位)
题目中说明库存量和总售价为 N 个正数 所以不一定是整数,可能是浮点数
AC代码(C++)
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
struct yuebing
{
double kucun; // 库存
double zongshoujia; // 总售价
double danjia; // 单价
};
bool cmp(struct yuebing a, struct yuebing b)
{
// 单价高的排前面
return a.danjia > b.danjia;
}
int main()
{
int num; // 月饼种类
int maxNeed; // 市场最大需求量
cin >> num >> maxNeed;
// 动态申请空间
struct yuebing* ptr = new struct yuebing[num];
for (int i = 0; i < num; i++)
cin >> ptr[i].kucun;
for (int i = 0; i < num; i++)
{
cin >> ptr[i].zongshoujia;
// 计算月饼单价
ptr[i].danjia = double(ptr[i].zongshoujia) / ptr[i].kucun;
}
// 用月饼单价降序排列
sort(ptr, ptr + num, cmp);
int k = 0; // 用于遍历结构体
double ans = 0; // 保存计算出来的结果
while (maxNeed > 0) // 市场需求未满足时继续计算
{
// 市场需求未满足但是所有种类月饼已卖完
if (k >= num)
// 已经没有月饼了所以停止计算
break;
// 当前种类月饼库存能满足市场需求
if (maxNeed < ptr[k].kucun)
{
// 从库存中减去最后一批月饼,在本题中这一行代码没有意义
ptr[k].kucun -= maxNeed;
// 计算最后一批月饼的收益并加入总收益
ans += ptr[k].danjia * maxNeed;
// 最后一批月饼已卖出市场需求置0
maxNeed = 0;
}
else
{
// 投入当前月饼的所有库存填补市场,减少市场需求
maxNeed -= ptr[k].kucun;
// 把当前月饼的总收益加入统计的总收益中
ans += ptr[k].zongshoujia;
}
k++; // 遍历月饼种类结构体
}
// 留两位数点
cout << fixed << setprecision(2) << ans;
return 0;
}
测试点2:库存浮点数情况
不同过的朋友可以试试下面的测试数据。
输入:
3 20
18.2 15.4 10
75.6 72.7 45.6
输出:
90.94
测试点3:市场需求大于所有月饼的总库存,也就是说把所有月饼卖了也无法满足市场需求的情况
出现段错误在朋友可以试试下面的测试数据
输入:
3 200
18.2 15.4 10
75.6 72.7 45.6
输出:
193.90
边栏推荐
- How to get free traffic in pinduoduo new store and what links need to be optimized in order to effectively improve the free traffic in the store
- Taobao Commodity details page API interface, Taobao Commodity List API interface, Taobao Commodity sales API interface, Taobao app details API interface, Taobao details API interface
- R language [logic control] [mathematical operation]
- 产业金融3.0:“疏通血管”的金融科技
- Bat instruction processing details
- Type de texte de commutation d'entrée et de mot de passe de l'applet natif
- Realize GDB remote debugging function between different network segments
- Determine whether the file is a DICOM file
- 【Shell】清理nohup.out文件
- 常用消息队列有哪些?
猜你喜欢

How digitalization affects workflow automation

集群、分布式、微服务的区别和介绍

Introduction to distributed transactions

Forkjoin is the most comprehensive and detailed explanation (from principle design to use diagram)

京东商品详情页API接口、京东商品销量API接口、京东商品列表API接口、京东APP详情API接口、京东详情API接口,京东SKU信息接口

Flink SQL realizes reading and writing redis and dynamically generates hset key

AI face editor makes Lena smile

分布式事务介绍

What are the common message queues?

Harmonyos practice - Introduction to development, analysis of atomized services
随机推荐
Nodejs get client IP
如何提高网站权重
什么是消息队列?
Explication contextuelle du langage Go
Randomly generate session_ id
数据中心为什么需要一套基础设施可视化管理系统
目标检测中的损失函数与正负样本分配:RetinaNet与Focal loss
成为资深IC设计工程师的十个阶段,现在的你在哪个阶段 ?
I didn't know it until I graduated -- the principle of HowNet duplication check and examples of weight reduction
Reading the paper [sensor enlarged egocentric video captioning with dynamic modal attention]
谈fpga和asic的区别
爬虫练习题(三)
Industrial Finance 3.0: financial technology of "dredging blood vessels"
Three level menu data implementation, nested three-level menu data
Message queuing: how to ensure that messages are not lost
zabbix_get测试数据库失败
Common skills and understanding of SQL optimization
Web authentication API compatible version information
JD commodity details page API interface, JD commodity sales API interface, JD commodity list API interface, JD app details API interface, JD details API interface, JD SKU information interface
Web architecture design process