解题思路:一只老鼠共有m的猫粮,给出n个房间,每一间房间可以用f[i]的猫粮换取w[i]的豆,问老鼠最多能够获得豆的数量 sum

即每一间房间的豆的单价为v[i]=f[i]/w[i],要想买到最多的豆,一定是先买最便宜的,再买第二便宜的,再买第三便宜的

-----m的值为0的时候求得的sum即为最大值   所以先将v[i]从小到大排序。

 

FatMouse' Trade

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

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
#include<stdio.h>
void bubblesort(double v[],int w[],int f[],int n)
{
int i,j;
double t;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(v[i]>v[j])
{
t=v[i];
v[i]=v[j];
v[j]=t; t=f[i];
f[i]=f[j];
f[j]=t; t=w[i];
w[i]=w[j];
w[j]=t;
}
}
}
}
int main()
{
int n,m,i,j,w[1000],f[1000];
double v[1000],sum;
while(scanf("%d %d",&m,&n)!=EOF&&(n!=-1)&&(m!=-1))
{
for(i=1;i<=n;i++)
{
scanf("%d %d",&w[i],&f[i]);
v[i]=f[i]*1.0/w[i];
}
bubblesort(v,w,f,n); sum=0;
for(i=1;i<=n&&v[i]<=m&&m>0;i++) //如果v[i]>m,则单价大于总价,不能进行交换,跳出循环
{ if(m>=f[i]) //总价能够买到该房间所有的豆
{
sum+=w[i];
m=m-f[i];
}
else
{
sum+=m*1.0/v[i]; //总价只能买到该房间部分的豆,说明买完这间房间的豆,总价也用完了
m=0;
}
}
printf("%.3lf\n",sum);
}
}

  

HDU 1009 FatMouse' Trade【贪心】的更多相关文章

  1. HDU 1009 FatMouse' Trade(贪心)

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

  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' Trade(贪心)

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

  5. HDOJ.1009 FatMouse' Trade (贪心)

    FatMouse' Trade 点我挑战题目 题意分析 每组数据,给出有的猫粮m与房间数n,接着有n行,分别是这个房间存放的食物和所需要的猫粮.求这组数据能保证的最大的食物是多少? (可以不完全保证这 ...

  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 2016-05-05 23:02 86人阅读 评论(0) 收藏

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

  8. 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) ...

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

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

随机推荐

  1. CentOS 7添加开机启动服务/脚本

    一.添加开机自启服务 在CentOS 7中添加开机自启服务非常方便,只需要两条命令(以 jenkins 为例):systemctl enable jenkins.service #设置jenkins服 ...

  2. hibernate详细配置

    映射配置 <!-- 映射文件: 映射一个实体类对象:  描述一个对象最终实现可以直接保存对象数据到数据库中.  --> <!-- package: 要映射的对象所在的包(可选,如果不 ...

  3. 路飞学城Python-Day113

      107-HTTP协议的无状态保存 什么是无状态保存? HTTP无状态请求就是客户端每次发送的请求都是单独的新请求,每一次请求都是独立的,这样的特点在网站上就是服务器登录的时候记录浏览器的信息,建立 ...

  4. Django:URL,Views,Template,Models

    准备工作:熟悉Django命令行工具 django-admin.py 是Django的一个用于管理任务的命令行工具,常用的命令整理如下: <1> 创建一个django工程 : django ...

  5. C++进阶 STL(1) 第一天 [容器,算法,迭代器] string容器 vector容器 deque容器

    课程大纲 02实现基本原理 容器,算法,迭代器 教室:容器 人:元素 教室对于楼:容器 序列式容器: 容器元素在容器中的位置是由进入容器的时间和地点来决定 序列式容器 关联式容器: 教室中 按年龄排座 ...

  6. js中数组常用方法

    1.Array.push() 此方法是在数组的后面添加新加元素,此方法改变了数组的长度: var aa=[1,2,3]; var bb=aa.push(4,5); console.log(bb)    ...

  7. nyoj2-吝啬的国度

    吝啬的国度 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 在一个吝啬的国度里有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来.现在,Tom在第S号城市,他有 ...

  8. 使用shell脚本定时执行备份mysql数据库

    使用shell脚本定时执行备份mysql数据库 #!/bin/bash ############### common file ################ #本机备份文件存放目录 MYSQLBA ...

  9. vim+astyle安装使用

    astyle下载安装 wget https://sourceforge.net/projects/astyle/files/astyle/astyle%203.1/astyle_3.1_linux.t ...

  10. prim求最小生成树

    一直以来只会Kruskal prim和dijkstra很像 只不过prim维护的是最短的边,而dijkstra维护的是最短的从起点到一个点的路径 同时prim要注意当前拓展的边是没有拓展过的 可以用堆 ...