题目:

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. Delphi Socket Demo

    Delphi Socket Demo 转自  http://www.anqn.com/dev/delphi/2010-01-07/a09122531-1.shtml   自己对中间有点修改,下面是代码 ...

  2. VC中利用多线程技术实现线程之间的通信

    当前流行的Windows操作系统能同时运行几个程序(独立运行的程序又称之为进程),对于同一个程序,它又可以分成若干个独立的执行流,我们称之为线程,线程提供了多任务处理的能力.用进程和线程的观点来研究软 ...

  3. poj2000---和1969一样的模板

    #include <stdio.h> #include <stdlib.h> int main() { int d; while(scanf("%d",&a ...

  4. 深入理解java虚拟机---读后笔记(垃圾回收)

    运行时数据区,主要包括方法区.虚拟机栈.本地方法栈.堆.程序计数器,该部分内存都是线程隔离的. 然后和其交互的有执行引擎.本地库接口,此部分线程之间是可以共享的. 1. 引用计数算法 给对象添加一个引 ...

  5. 【POJ】2492 A bug's life ——种类并查集

    A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 28211   Accepted: 9177 De ...

  6. CodeForces 135C C. Zero-One

    题目 题意: 一个01串,AB两个人轮流删去一个字符,直到只剩两个,A先手.最后剩的两位组成一个二进制数,A要使其最小,B要使其最大. 有一些部分不知道原来是什么,用?表示,求所有的可能里,最后剩下的 ...

  7. Introduction the naive“scull” 《linux设备驱动》 学习笔记

    Introduction the naive "scull" 首先.什么是scull? scull (Simple Character Utility for Loading Lo ...

  8. Android 启动Activity的方式

    Activity的启动分为两种方式,显示方式和隐式方式,显示方式就是在通过intent启动Activity时指定了Activity的包名和类名. 而隐式方式则在初始化Intent时仅仅指定action ...

  9. Ext.Net 使用总结之GridPanel的删除事件

    1.关于Ext.net中GridPanel的删除事件 首先是GridPanel,如下: <ext:GridPanel ID="GridPanel1" runat=" ...

  10. 几个SQL

    select sum(`value`) from testtable where value != 'error' AND type ='b' in (select DISTINCT(type) fr ...