hdu 1009 FatMouse' Trade
2015-08-26 14:18
295 查看
FatMouse' Trade
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 54549 Accepted Submission(s): 18288
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1
[align=left]Sample Output[/align]
13.333
31.500
[align=left]Author[/align]
CHEN, Yue
[align=left]Source[/align]
ZJCPC2004
[align=left]Recommend[/align]
JGShining | We have carefully selected several similar problems for you: 1051 1052 1007 1006 1053
一道基础的贪心,只需排序就好,运用了sort排序和结构体,感觉十分方便和强大。
题意:老鼠准备了M磅猫食,准备拿这些猫食跟猫交换自己喜欢的食物。有N个房间,每个房间里面都有食物。你可以得到J[i]但你需要付出F[i]的猫食。要你计算你有M磅猫食可以获得最多食物的重量。食物可以被分割。
附上代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct node { int a,b; double c; }ss[1005]; bool cmp(node x,node y) { return x.c>y.c; } int main() { int n,m,i,j,k,w; double s; while(cin>>n>>m) { if(n==-1&&m==-1) break; s=0; for(i=0;i<m;i++) { cin>>ss[i].a>>ss[i].b; ss[i].c=(double)ss[i].a/(double)ss[i].b; //求性价比,每元钱可以换多少这种食物 } sort(ss,ss+m,cmp); //按性价比大小排序,单价买的越多,性价比越高 for(i=0;i<m;i++) { if(n>=ss[i].b) //拥有的钱超过这个食物全部的价格 { s+=ss[i].a; //全部食物的重量 n-=ss[i].b; //减去花费的钱 } else //若不够买这种食物的全部 { s+=ss[i].c*n; //花光所有的钱购买这种食物 break; //钱为0,跳出循环 } } printf("%.3lf\n",s); //保存3位小数输出 } return 0; }
相关文章推荐
- python wraps装饰器
- 浅析数据结构-图的基本概念
- MSChart的初步使用和学习
- hdoj5256序列变换【LIS】
- 2015年最新苹果开发者账号注册流程详解
- PHP抓取远程图片到本地
- USB中的描述符详解【一】
- iOS Resources
- Subsets C#
- oracle 创建表分区
- STM32停止模式唤醒不恢复时钟的后果
- [python] TCP客户端/服务器端
- 解决eclipse中出现Resource is out of sync with the file system问题
- 如何实现给字符串加千分符 例如:123456789.01 转换 123,456,789.01
- 使用图形化界面部署WSP解决方案包
- 十六进制颜色代码
- Android--显示指定路径的图片
- 【Java关键字-Interface】为什么Interface中的变量只能是 public static final
- PullToRefreshListView中如何addHeaderView
- 计时器Timer,动画