FatMouse' Trade

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 43381    Accepted Submission(s):
14499

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
 
Author
CHEN, Yue
 
贪心题:题目的意思是FatMouse有m磅的cat food,它想用这些cat food去换JavaBean,然后有n个房间,每个房间有相应的JavaBean和需要多少cat food。问m磅cat food最多可以换得多少JavaBean?
首先要将这些数据按照性价比(即JavaBean除以cat food 注意性价比有可能是小数)从大到小排序,然后问题就迎刃而解了。
 
 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<iomanip>
using namespace std;
#define N 1005 struct Trade{
int J, F; //J代表JavaBeans,F代表cat food
double price; //性价比
}trade[N]; int cmp(Trade a,Trade b)
{
return a.price>b.price;
}
int main()
{
double maximum;
int m, n, i;
while(cin>>m>>n)
{
maximum = ;
if(m==- && n==-)
break;
maximum = ;
for(i=; i<n; i++)
{
cin>>trade[i].J>>trade[i].F;
trade[i].price = trade[i].J*1.0/trade[i].F;
}
sort(trade,trade+n,cmp);
for(i=; i<n; i++)
{
if(m == )
break;
else if(trade[i].F<=m)
{
maximum += trade[i].J;
m -= trade[i].F;
}
else
{
maximum += m*trade[i].price;
m = ;
}
}
printf("%.3lf\n",maximum);
}
return ;
}
 

Hdu 1009 FatMouse' Trade的更多相关文章

  1. hdu 1009 FatMouse&#39; Trade

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. HDU 1009 FatMouse' Trade(简单贪心)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1009 FatMouse' Trade Time Limit: 2000/1000 MS (Java/O ...

  3. HDU 1009 FatMouse' Trade(简单贪心 物品可分割的背包问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1009 FatMouse' Trade Time Limit: 2000/1000 MS (Java/O ...

  4. HDU 1009:FatMouse&#39; Trade(简单贪心)

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. [题解]hdu 1009 FatMouse' Trade(贪心基础题)

    Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding th ...

  6. hdu 1009:FatMouse' Trade(贪心)

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  7. Hdu 1009 FatMouse' Trade 分类: Translation Mode 2014-08-04 14:07 74人阅读 评论(0) 收藏

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. FatMouse&#39; Trade(杭电1009)

    FatMouse' Trade Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tot ...

  9. HDU 1009 FatMouse' Trade(贪心)

    FatMouse' Trade Problem Description FatMouse prepared M pounds of cat food, ready to trade with the ...

随机推荐

  1. Android入门(三):使用TextView、EditText 和Button接口组件

    我使用的IDE是Android Studio 2.1,虽然使用Eclipse也可以进行Android的开发,但是网上的大神大都推荐Android Studio,愿意了解的朋友可以参考知乎上关于Andr ...

  2. 如何將ViewData裡包含的Html輸出(MVC)

    如何將ViewData裡包含的Html輸出(MVC) 默認輸入ViewData裡的Htm系統會自動把標籤轉換而達不到預覽的效果, 我們如果要呈現解析後的HTML則要調用@MvcHtmlString.C ...

  3. docker 配置文件引发的问题

    好久没有配置 vmware / harbor 了,突然间来了兴趣,结果让我失望了,登陆反复的被refused; 这个是配置文件地址:https://github.com/vmware/harbor/b ...

  4. ZeroMQ接口函数之 :zmq_msg_close – 释放一个ZMQ消息

    ZeroMQ 官方地址 :http://api.zeromq.org/4-2:zmq_msg_close zmq_msg_close(3)            ØMQ Manual - ØMQ/3. ...

  5. 浅析mongodb中group分组

    这篇文章主要介绍了浅析mongodb中group分组的实现方法及示例,非常的简单实用,有需要的小伙伴可以参考下. group做的聚合有些复杂.先选定分组所依据的键,此后MongoDB就会将集合依据选定 ...

  6. Java并发编程底层实现原理 - volatile

    Java语言规范第三版中对volatile的定义如下: Java编程语言允许线程访问共享变量,为了确保共享变量能被准确和一致性的更新,线程应该确保通过排他锁 单独获得这个变量. volatile有时候 ...

  7. 第一章-第三题(目前流行的源程序版本管理软件和项目管理软件优缺点)--By梁旭晖

    引用自:http://www.cnblogs.com/WJ1234/p/5285595.htmlhttp://blog.163.com/yuyang_tech/blog/static/21605008 ...

  8. 【转】自学成才秘籍!机器学习&深度学习经典资料汇总

      小编都深深的震惊了,到底是谁那么好整理了那么多干货性的书籍.小编对此人表示崇高的敬意,小编不是文章的生产者,只是文章的搬运工. <Brief History of Machine Learn ...

  9. nodejs如何储存一个GBK编码的文件

    思路:utf-8 -> decode(to buffer) -> convert to gbk(buffer also) -> write buffer to file. var f ...

  10. 让javascript显原型!

    相信以下的javascript让你读起来痛苦不已,告诉你一下简单的办法,就可以让它显出原型!将第一个单词,即eval换成document.write,然后再运行一下,它立即就原形毕露了! eval(f ...