题目:

FatMouse' Trade

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

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
 
分析:本题用到的是贪心算法,猫粮换成Java豆的比例越大应越先被兑换。在此我通过每个结构体保存每个屋子中的J[i]与F[i]及其兑换比例scale,然后利用sort将所有结构体中的scale按从大到小进行排序。之所以这样做的原因是,根据scale排序后,不会打乱原先每个屋子J[i]与F[i]及其兑换比例scale的对应关系,即排序的过程中结构体的结构不发生变换,只不过是根据结构体中的scale变量给所有结构体排一下序而已。最后的换Java豆的工作也简单多了。
 
代码如下:
 #include<cstdio>
#include<algorithm>
using namespace std; const int maxN = + ; struct warehouse{
int J;
int F;
double scale;
}House[maxN]; bool cmp(const struct warehouse a, const struct warehouse b) {
return a.scale > b.scale;
} int main() {
int M, N;
double ans;
while(scanf("%d %d", &M, &N) == ){
if(M == - && N == -) break;
//输入
for(int i = ; i < N; i++) {
scanf("%d %d", &House[i].J, &House[i].F);
House[i].scale = (double)House[i].J/House[i].F;
}
//将所有屋子中的猫粮与Java豆兑换的比例排序
sort(House, House + N, cmp);
// for(int i = 0; i < N; i++)
// printf("%.3lf\t", House[i].scale);
//按比例从大到小分配猫粮
ans = 0.0;
int pos = ;
while(M > && N > ){//猫粮换完,或者Java豆已经没有时应该终止循环
if(M > House[pos].F)
ans += House[pos].J; //若猫粮充足,直接将屋子的Java豆兑换下来
else
ans += (double)House[pos].J * M / House[pos].F; //能兑换的猫粮不足,这时应该按比例来兑换Java豆
M -= House[pos].F;
N--;
pos++;//到下一家
}
//输出
printf("%.3lf\n", ans);
}
return ;
}

                                                                2015-07-02文

HDOJ----------1009的更多相关文章

  1. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

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

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

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

  3. 【HDOJ 1009】 CRB and String

    [HDOJ 1009] CRB and String 每组两个串s t 仅仅由小写字母组成 问从s能不能变成t 改变的操作为选一个字符 在后面加上一个与所选字符不同的字符 这样的操作能够做无数次 问能 ...

  4. Hdoj 1009.FatMouse' Trade 题解

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

  5. 【HDOJ】1009 FatMouse' Trade

    这道题目是一道非常简单的贪心,但是我却修改了1h+.原因就是qsort的comp有bug.其实还是题目中的数据可以为0.除数为0真的要慎重啊.后来改为结构体,加一层循环选取最大值,果然ac啊.wa了几 ...

  6. 杭电hdoj题目分类

    HDOJ 题目分类 //分类不是绝对的 //"*" 表示好题,需要多次回味 //"?"表示结论是正确的,但还停留在模块阶 段,需要理解,证明. //简单题看到就 ...

  7. HDOJ 题目分类

    HDOJ 题目分类 /* * 一:简单题 */ 1000:    入门用:1001:    用高斯求和公式要防溢出1004:1012:1013:    对9取余好了1017:1021:1027:   ...

  8. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  9. HDOJ 1326. Box of Bricks 纯水题

    Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  10. 1009: [HNOI2008]GT考试

    1009: [HNOI2008]GT考试 Time Limit: 1 Sec Memory Limit: 162 MB Description 阿申准备报名参加GT考试,准考证号为N位数\(X_1X_ ...

随机推荐

  1. FineUI_动态绑定Grid

    private void InitGrid() { string _sql = GetSql().ToLower().Replace("select", "") ...

  2. centos 6.7 perl 版本 This is perl 5, version 22 安装DBI DBD

    <pre name="code" class="cpp">centos 6.7 perl 版本 This is perl 5, version 22 ...

  3. vagrant打造自己的开发环境

    vagrant打造自己的开发环境 缘由: 在网上看到斌哥,爽神都写了关于vagrant的博客,都在说很强大,所以很好奇这玩意怎么个强大,然后也就自己来一发玩玩看看. 真实缘由: 说实话是电脑配置太低, ...

  4. flash跨域策略文件crossdomain.xml

    flash在跨域时唯一的限制策略就是crossdomain.xml文件,该文件限制了flash是否可以跨域读写数据以及允许从什么地方跨域读写数据. 位于www.a.com域中的SWF文件要访问www. ...

  5. PHPExcel 多工作表 导出

    //浏览器输出excel header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet ...

  6. poj2389---大数乘法

    #include<stdlib.h> #include<stdio.h> #include<string.h> #define MAX 100 int main() ...

  7. 理解session机制

    理解session机制 session机制是一种服务器端的机制,服务器使用一种类似于散列表的结构(也可能就是使用散列表)来保存信息. 当程序需要为某个客户端的请求创建一个session的时候,服务器首 ...

  8. 关于cocos2d安装时编译不成功(个人心得)

    在解压cocos2d执行vs2010.sln时错误发生不能成功生成.遇到这样的错误: 1>c:\program files\microsoft sdks\windows\v7.0a\includ ...

  9. 利用aspose.cell把数据导出到excel

    /// <summary> /// 导出数据到本地 /// </summary> /// <param name="dt">要导出的数据< ...

  10. 前端新人学习笔记-------html/css/js基础知识点(二)

    4月7日学到的知识点:     一:<img src="1.png" alt="美女"/> alt是给图片添加介绍,当图片没加载出来时,会直接显示a ...