博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FatMouse' Trade
阅读量:4346 次
发布时间:2019-06-07

本文共 2116 字,大约阅读时间需要 7 分钟。

FatMouse' Trade

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 92544    Accepted Submission(s): 32114

Problem Description
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.
 

 

Input
The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1's. All integers are not greater than 1000.
 

 

Output
For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.
 

 

Sample Input
5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1
 

 

Sample Output
13.333 31.500
 
 
解题思路:贪心,开一个结构体后按照平均值从大到小排序,先换掉平均值大的房间里的JavaBean,输出用printf输出,用setpricision会wa,原因在于setprecision会四舍五入丢失精度。
#include
#include
using namespace std;struct jb{ double j,f,avg;};bool cmp(jb x,jb y){ return x.avg
>n>>m&&n!=-1) { jb s[1001]; for(int i=0;i
>s[i].j>>s[i].f;s[i].avg=s[i].j/s[i].f;} sort(s,s+m,cmp); double sum=0; for(int i=m-1;i>=0;--i) { if(n>=s[i].f) { n-=s[i].f; sum+=s[i].j; } else if(n
0) { sum+=s[i].avg*n; break; } } printf("%.3lf\n",sum); //cout<
<
<
<

 

转载于:https://www.cnblogs.com/wjw2018/p/9321184.html

你可能感兴趣的文章
[Javascirpt] What’s new in JavaScript (Google I/O ’19)
查看>>
[Angular 2] Writing a Simple Angular 2 Component
查看>>
可能会用的到的JQ插件
查看>>
高斯消元模板
查看>>
【GPS】SAP测试GPS模块拿不到sensor数据
查看>>
python 数据类型之列表、元组、字典、集合
查看>>
【Java并发编程】8、各种锁的概念
查看>>
【Java基础】5、java中的匿名内部类
查看>>
Python中capitalize()与title()的区别
查看>>
GRASP (职责分配原则)
查看>>
C#语言特性-运算符重载
查看>>
echart.js的使用
查看>>
IC 设计中DFT的Boundary Scan功能
查看>>
iOS 2D绘图详解(Quartz 2D)之Bitmap
查看>>
Swift - 让程序挂起后,能在后台继续运行任务
查看>>
Python3基本语法
查看>>
【 PostgreSQL】后台周期执行函数实例(shell+crontab)
查看>>
python操作TexturePacker批量打包资源plist png
查看>>
lua性能篇,还没时间看,先保存一下
查看>>
教你手动挡驾驶技术如何提高驾车技巧
查看>>