题目描述:

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

注意:销售时允许取出一部分库存。样例给出的情形是这样的:假如我们有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

tips:本题在乙级中属于重点题目,并且不难,只是有点麻烦。属于结构体排序问题。定义结构体,定义结构体排序函数。
主函数中可以保证获得最大的利润。
 #include<bits/stdc++.h>
using namespace std;
struct N{
double num;
double total;
double price;
}data[]; bool cmp(N a,N b){
return a.price>b.price;
}
int main(){
int n,m;
scanf("%d%d",&n,&m); for(int i=;i<n;i++)
scanf("%lf",&data[i].num); for(int i=;i<n;i++)
scanf("%lf",&data[i].total);
for(int i=;i<n;i++)
data[i].price=data[i].total/data[i].num;
sort(data,data+n,cmp);
double x=;
int j=;
while(m>&&j<n){
if(m>data[j].num){
m-=data[j].num;
x+=data[j].total;
}
else{ x+=data[j].price*m;
m-=data[j].num;
}
j++; } printf("%.2lf\n",x); return ;
}

PAT-B-1020 月饼 (25)(25 分)的更多相关文章

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

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

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

    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 Tree Traversals (25)(25 分)

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  5. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  6. PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  7. PAT Advanced 1020 Tree Traversals (25 分)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  8. PAT Basic 1020 月饼 (25 分)

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

  9. PAT (Basic Level) Practice 1020 月饼 分数 25

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

  10. 【PAT】1020. Tree Traversals (25)

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

随机推荐

  1. 【spring源码分析】IOC容器初始化(一)

    前言:spring主要就是对bean进行管理,因此IOC容器的初始化过程非常重要,搞清楚其原理不管在实际生产或面试过程中都十分的有用.在[spring源码分析]准备工作中已经搭建好spring的环境, ...

  2. centos6+ 安装python3.6以及pip3

    https://blog.csdn.net/weixin_42350212/article/details/83008248

  3. linux基本命令2

    目录: 目录: /bin:可执行文件 /sbin:系统文件 /dev:设备文件 命令: mkdir test cd test touch 11.txt vi 11.txt cat 11.txt cp ...

  4. hashChange & url change & QRCode & canvas to image

    hashChange & url change & QRCode & canvas to image "use strict"; /** * * @auth ...

  5. Vue.js 2.x笔记:路由Vue Router(6)

    1. Vue Router简介与安装 1.1 Vue Router简介 Vue Router 是 Vue.js 官方的路由管理器.它和 Vue.js 的核心深度集成,构建单页面应用. Vue Rout ...

  6. tomcat server.xml结构

    所有xml文件使用的文件头 <?xml version='1.0' encoding='utf-8'?> 2 <Server port="8005" shutdo ...

  7. CountDownLatch类实现同步

    首先我们看一个普通的多线程代码 class MyThread implements Runnable { @Override public void run() { System.out.printl ...

  8. Jetson TX1安装pyTorch

    https://www.ncnynl.com/archives/201705/1629.html https://blog.csdn.net/qq_36302589/article/details/8 ...

  9. git学习02 - log查看&版本回退

    1.查看更新记录 git log /  git log --pretty=oneline D:\learn\git_test>git log commit a915e7b12076673d778 ...

  10. Mysql高性能笔记(一):Schema与数据类型优化

    1.数据类型 1.1.几个参考优化原则 a.  更小的通常更好 i.更小的数据类型,占用更少磁盘.内存和CPU缓存,需要的CPU周期更少 ii.如果无法确定哪个数据类型是最好的,就选择不会超过范围的最 ...