当前位置:网站首页>PAT 乙等 1020.月饼
PAT 乙等 1020.月饼
2022-06-23 04:11:00 【章鱼bro】
1020. 月饼 (25)
月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需求量,请你计算可以获得的最大收益是多少。
注意:销售时允许取出一部分库存。样例给出的情形是这样的:假如我们有3种月饼,其库存量分别为18、15、10万吨,总售价分别为75、72、45亿元。如果市场的最大需求量只有20万吨,那么我们最大收益策略应该是卖出全部15万吨第2种月饼、以及5万吨第3种月饼,获得 72 + 45/2 = 94.5(亿元)。
输入格式:
每个输入包含1个测试用例。每个测试用例先给出一个不超过1000的正整数N表示月饼的种类数、以及不超过500(以万吨为单位)的正整数D表示市场最大需求量。随后一行给出N个正数表示每种月饼的库存量(以万吨为单位);最后一行给出N个正数表示每种月饼的总售价(以亿元为单位)。数字间以空格分隔。
输出格式:
对每组测试用例,在一行中输出最大收益,以亿元为单位并精确到小数点后2位。
输入样例:3 20 18 15 10 75 72 45输出样例:
94.50
思路:将所有月饼按照单价进行降序排列,从高到低进行销售,若总需求大于某种月饼的库存,则全部销售,否则按照剩余需求占月饼库存的百分比售出
一、起始变量
1.结构体moonCake含有库存,总售价,单价
2.N月饼种类,D总需求
3.sale总销售额
二、运算
1.循环输入各种月饼的库存、总售价并计算出单价
2.按照单价递减的顺序进行排序
3.若总需求大于某种月饼的库存,则用总销售额加上此种月饼的总售价,若剩余总需求小于某种月饼的库存,将剩余总需求占库存的百分比乘以此种月饼的总售价进行出售
代码:
//18:00
#include "stdio.h"
#include "stdlib.h"
typedef struct {
double inventory; //库存
double price; //总售价
double sigalPrice; //单价
}moonCake;
int cmp(const void * a, const void * b);
int main()
{
int N,D;
scanf("%d %d",&N,&D);
moonCake moonCakes[N];
double sale = 0;
for(int i = 0; i < N; i++)
{
scanf("%lf",&moonCakes[i].inventory); //循环输入各种月饼的库存
}
for(int i = 0; i < N; i++)
{
scanf("%lf",&moonCakes[i].price); //循环输入各种月饼的售价
moonCakes[i].sigalPrice = moonCakes[i].price / moonCakes[i].inventory; //计算单价
}
qsort(moonCakes,N,sizeof(moonCakes[0]),cmp); //按照单价递减排序
int i = 0;
while(D && i != N)//当总需求不为零并且还有月饼没有售出时
{
if(D > moonCakes[i].inventory) //总需求大于第i种月饼的库存
{
sale += moonCakes[i].price;
D -= moonCakes[i].inventory;
}
else //总需求小于第i种月饼的库存
{
sale += moonCakes[i].price * (D / moonCakes[i].inventory); //用剩余总需求占第i中月饼的百分比乘以第i种月饼的库存,相当于总需求全部满足D=0
D -= D;
}
i++;
}
printf("%.2lf",sale);
return 0;
}
int cmp(const void * a, const void * b)
{
return ((moonCake *)a)->sigalPrice > ((moonCake *)b)->sigalPrice ? -1 : 1;
}边栏推荐
- ArcTime 制作中英文字幕视频
- Design and implementation of spark offline development framework
- Jvm: when a method is overloaded, the specific method to call is determined by the static type of the incoming parameter rather than the actual type of the parameter
- Use of visdom
- PAT 乙等 1025 反转链表
- Visdom draws multiple dynamic loss curves
- Management system of borrowed books based on SSM framework
- TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.3 全双工, 节能, 自动协商机制, 802.1X 流控制 / 3.3.3 链路层流量控制
- How much disk IO will actually occur for a byte of the read file?
- Differences between fs4059a and fs5080e charging chips
猜你喜欢

技能自检 | 想当测试Leader,这6项技能你会吗?

Is there a real part-time job online? How do college students find part-time jobs in summer?

Redis cache penetration solution - bloom filter

MySQL面试真题(二十四)——行列互换

数字化工厂建设可划分为三个方面

数字藏品到底有什么魔力?目前有哪些靠谱的团队在开发

Leetcode 797: all possible paths

iNFTnews | 加密之家从宇宙寄来的明信片,你会收到哪一张?

Raspberry pie assert preliminary exercise

The road of architects starts from "storage selection"
随机推荐
What does the English letter PC mean? What does the Internet PC mean
【斯坦福计网CS144项目】Lab2: TCPReceiver
英集芯IP5566带TYPE-C口3A充放快充移动电源5w无线充二合一方案SOC
常用的无线充发射IC芯片
Mobile phone wireless charging dual coil 15W scheme SOC IC ip6809
fastjson中的@JSONField注解
Webrtc[47] - a common way for webrtc to save YUV data
Ip6809 three coil 15W wireless charging transmitter scheme IC British chip
PAT 乙等 1011 C语言
AHA C language Chapter 7 you can do more with it (talks 27-28)
Real MySQL interview question (30) -- shell real estate order analysis
IP6809三线圈15W无线充电发射端方案ic英集芯
Jvm: when a method is overloaded, the specific method to call is determined by the static type of the incoming parameter rather than the actual type of the parameter
ssm项目搭建
Design and implementation of spark offline development framework
英集芯推出4串锂电池100W移动电源升降压方案SoC芯片IP5389
AI艺术的基因工程?使用 #Artbreeder 改变图像的任意形态
Alibaba cloud object storage oss+picgo+typera implements the construction map
[proteus simulation] Arduino uno+pcf8574+lcd1602+mpx4250 electronic scale
Genetic engineering of AI art? Use # artbreeder to change any shape of the image