题目:

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. Nginx模块开发入门(转)

    前言 Nginx是当前最流行的HTTP Server之一,根据W3Techs的统计,目前世界排名(根据Alexa)前100万的网站中,Nginx的占有率为6.8%.与Apache相比,Nginx在高并 ...

  2. poj2509---抽k根烟就换一支,求能抽烟的总数

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

  3. Saiku如何固定查询结果table的表头和首列

    在使用saiku查询的时候,当“行”和“列”的维度内容过多时,在查看时只看到数据,不知道是什么数据,维度不清楚,得来回拖动滚动条才行,所以同事提出想要固定“表头”和“首列”. 在网上找了一些现成的插件 ...

  4. pywebkitgtk安装出现的问题

    configure 文件里 print sys.prefix 等不能支持python3的原因 依据http://blog.csdn.net/jklfjsdj79hiofo/article/detail ...

  5. oracle归档日志

    前几天因为导入大的东西,弄得很久都没动静,一看最后才发现是归档满了.但是很多的命令还是很是很不熟悉,所以看了下,百度了下.整理下这个. 1.查看归档日志大小及使用情况 select * from v$ ...

  6. Winform中上传、下载文件选择打开文件的位置

    打开将要上传的文件 var fileName="";OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.Mul ...

  7. Mybatis设置自增主键

    useGeneratedKeys="true" keyProperty="id" 方法1: <insert id="insert" p ...

  8. 中国 省会 地级市 经纬度 city array

    <?php $city_arr = array ( '北京' => array ( 'gis_lng' => '116.405285', 'gis_lat' => '39.90 ...

  9. Java的序列化

    1.为啥需要序列化 在Java编程时,一个类被实例化以后,Java虚拟机使得对象处理生存状态,但是当虚拟机关闭后,对象就不复存在了,所以一个对象的生存期不会超过JVM的工作时间,那么如何才能让对象持续 ...

  10. C# 基础中有关术语理解

    一.栈vs堆  深入理解堆栈.堆在内存中的实现 二.Socket 深入探析c# Socket 三.多线程 c# 多线程 --Mutex(互斥锁)