当前位置:网站首页>Pat class B 1020 Moon Cake
Pat class B 1020 Moon Cake
2022-06-23 05:57:00 【Octopus bro】
1020. The moon cake (25)
Moon cake is a kind of traditional food that Chinese people eat during the Mid Autumn Festival , There are many different flavors of moon cakes in different regions . Now, the inventory of all kinds of mooncakes is given 、 Total selling price 、 And the biggest demand in the market , Please calculate the maximum profit you can get .
Be careful : Allow to take out part of the inventory when selling . The example shows this : If we have 3 Mooncakes , The inventory is 18、15、10 Ten thousand tons of , The total selling price is 75、72、45 One hundred million yuan . If the market's maximum demand is only 20 Ten thousand tons of , Then our biggest revenue strategy should be to sell all 15 Ten thousand tons 2 Mooncakes 、 as well as 5 Ten thousand tons 3 Mooncakes , get 72 + 45/2 = 94.5( One hundred million yuan ).
Input format :
Each input contains 1 Test cases . Each test case shall be provided with no more than one 1000 The positive integer N Number of moon cakes 、 And not more than 500( In 10000 tons ) The positive integer D Represents the maximum market demand . The next line shows N A positive number indicates the stock of each kind of moon cake ( In 10000 tons ); The last line gives N A positive number indicates the total selling price of each kind of moon cake ( In 100 million yuan ). Numbers are separated by spaces .
Output format :
For each group of test cases , Output maximum revenue in one line , In hundred million yuan and accurate to the decimal point 2 position .
sample input :3 20 18 15 10 75 72 45sample output :
94.50
Ideas : Arrange all moon cakes in descending order according to the unit price , Selling from high to low , If the total demand is greater than the inventory of a certain kind of moon cake , All sales , Otherwise, it will be sold according to the percentage of the remaining demand in the moon cake inventory
One 、 Starting variable
1. Structure moonCake Contains inventory , Total selling price , The unit price
2.N Types of moon cakes ,D Aggregate demand
3.sale Total sales
Two 、 operation
1. Cycle through the inventory of various mooncakes 、 Total selling price and calculate unit price
2. Sort by decreasing unit price
3. If the total demand is greater than the inventory of a certain kind of moon cake , The total sales plus the total selling price of this kind of moon cake , If the remaining total demand is less than the inventory of a certain kind of moon cake , Multiply the remaining total demand as a percentage of inventory by the total selling price of such mooncakes to sell
Code :
//18:00
#include "stdio.h"
#include "stdlib.h"
typedef struct {
double inventory; // stock
double price; // Total selling price
double sigalPrice; // The unit price
}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); // Cycle through the inventory of various mooncakes
}
for(int i = 0; i < N; i++)
{
scanf("%lf",&moonCakes[i].price); // Enter the selling price of various moon cakes in a cycle
moonCakes[i].sigalPrice = moonCakes[i].price / moonCakes[i].inventory; // Calculate unit price
}
qsort(moonCakes,N,sizeof(moonCakes[0]),cmp); // Sort by decreasing unit price
int i = 0;
while(D && i != N)// When the total demand is not zero and there are still moon cakes unsold
{
if(D > moonCakes[i].inventory) // The total demand is greater than i Stock of moon cakes
{
sale += moonCakes[i].price;
D -= moonCakes[i].inventory;
}
else // The total demand is less than i Stock of moon cakes
{
sale += moonCakes[i].price * (D / moonCakes[i].inventory); // The remaining total demand accounts for i Multiply the percentage of mid moon cakes by the number i Stock of moon cakes , It is equivalent to that the total demand is fully satisfied 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;
}边栏推荐
- App SHA1 acquisition program Baidu map Gaode map simple program for acquiring SHA1 value
- Leetcode topic analysis: factorial training zeroes
- The traditional Internet like platform may no longer exist, and a new industry integrating industrial characteristics and Internet characteristics
- [Stanford Jiwang cs144 project] lab2: tcpreceiver
- ORB_SLAM2运行
- What benefits have digital collections enabled the real industry to release?
- Advanced Mathematics (Seventh Edition) Tongji University exercises 1-7 personal solutions
- 华为软硬件生态圈成型,从根子上改变美国对软硬件体系的领导地位
- Pat class B 1012 C language
- What is the magic of digital collections? Which reliable teams are currently developing
猜你喜欢
![[OWT] OWT client native P2P E2E test vs2017 build 6: modify script automatic generation vs Project](/img/ce/2e0b0c0f6fae24b5b0df2680229284.png)
[OWT] OWT client native P2P E2E test vs2017 build 6: modify script automatic generation vs Project

android Handler内存泄露 kotlin内存泄露处理

The digital collection market has just begun

Adnroid activity截屏 保存显示到相册 View显示图片 动画消失

C prime plus notes d'apprentissage - 2, constantes et formatage io (I / o)

ant使用总结(二):相关命令说明

Visdom draws multiple dynamic loss curves

Real MySQL interview questions (XXVI) -- didi 2020 written examination questions

jvm-06. Garbage collector

A bit of knowledge - folding forging and Damascus steel
随机推荐
编址和编址单位
The difference between SaaS software and traditional software delivery mode
Prometheus, incluxdb2.2 installation and flume_ Export download compile use
TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.3 全双工, 节能, 自动协商机制, 802.1X 流控制 / 3.3.3 链路层流量控制
android Handler内存泄露 kotlin内存泄露处理
jvm-05.垃圾回收
阿里云 ACK One、ACK 云原生 AI 套件新发布,解决算力时代下场景化需求
ORB_SLAM2运行
The official artifact of station B has cracked itself!
Pat class B 1013 C language
Real MySQL interview questions (XXVI) -- didi 2020 written examination questions
TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.4 网桥与交换机
Wireshark TS | video app cannot play
Heimdall database proxy scale out 20 times
Real MySQL interview question (XXVIII) -- case - Analysis of indicators of communication operators
APP SHA1获取程序 百度地图 高德地图获取SHA1值的简单程序
Activity startup mode and life cycle measurement results
Oracle exception
PAT 乙等 1013 C语言
New classes are launched | 5 minutes each time, you can easily play with Alibaba cloud container service!