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. <一>获取数据库连接

    一.JDBC_通过Driver接口获取数据库连接 1. Driver是一个接口:数据库厂商必须提供实现的接口,可以从其中 获取数据库连接. 2.JDBC URL由三部分组成,各部分用冒号隔开,格式:j ...

  2. Oracle ITL(Interested Transaction List)理解

    ITL(Interested Transaction List) ITL是位于数据块头部的事物槽列表,它是由一系列的ITS(Interested Transaction Slot,事物槽)组成,其初始 ...

  3. NGUI实现技能CD效果

    在NGUI中使用Sprite的遮罩效果可以很轻松的实现技能CD效果. 具体实现步骤: ①新建一个技能图标的Sprite 如图中的Skill001,再在该技能Sprite上添加一个Sprite做遮罩, ...

  4. SQL语法

    full outer--全连.两表相同的组合在一起,A表有,B表没有的数据(显示为null),同样B表有,A表没有的显示为(null) A表 left join B表--左连,以A表为基础,A表的全部 ...

  5. jcl-over-slf4j log桥接工具简介

    ava 界里有许多实现日志功能的工具,最早得到广泛使用的是 log4j,许多应用程序的日志部分都交给了 log4j,不过作为组件开发者,他们希望自己的组件不要紧紧依赖某一个工具,毕竟在同一个时候还有很 ...

  6. PHP的变量

    1.可变变量 一个变量的变量名可以动态地设置和使用.一个普通的变量通过声明来设置,而一个可变变量获取了一个普通变量的值作为这个可变变量的变量名,如下所示: <?php $hi = "h ...

  7. 安卓中級教程(4):ScrollView與ListView之間的高度問題

    在scrollView中加插ListView是一個大難題.其中一個難題是Listview的高度難以計算,輸出效果往往強差人意,就讓我們看看當中的問題 . <LinearLayout xmlns: ...

  8. ExtJS笔记 Ext.data.Types

    This is a static class containing the system-supplied data types which may be given to a Field. Type ...

  9. nginx中相关配置

    #nginx 开启目录浏览 location / { root /data/www/file //指定实际目录绝对路径: autoindex on; //开启目录浏览功能: autoindex_exa ...

  10. 针对功能权限(url访问)如何避免越权访问

    你可以用request获得之前的页面路径:Request.getHeader("Referer");然后你可以判断一下,这个是字符串类型的. 如果是需要登录的,你可以从sessio ...