【九度OJ】题目1433:FatMouse 解题报告

标签(空格分隔): 九度OJ


http://ac.jobdu.com/problem.php?pid=1433

题目描述:

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.

输入:

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.

输出:

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.

样例输入:

5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1

样例输出:

13.333
31.500

Ways

开始贪心算法的题。

这个题目的意思是,M的cat foode去每个room里换JavaBean,每个房间都是按等比例换。

可以证明,只有把rate=JavaBean/catFoode值最大的先换了,才会使最终换的的javaBean最多。

也就是相当于换性价比最高的东西,这样就会总获得量最大。

这就需要排序,我在做这个题的时候遇到一个问题,是自己考虑的不周到。刚开始没有把每个房间当做一个整体,这样在rate排序之后,rate和房间对应不上。把room作为整体就能解决了。

第二个问题还是没有把answer在每个循环中清零。

#include <stdio.h>
#include <algorithm> using namespace std; struct room {
float cat;
float bean;
float rate;
} rooms[1000]; bool cmp(room a, room b) {
return a.rate > b.rate;
} int main() {
int m;
int n;
float answer;
while (scanf("%d", &m) != EOF && m != -1) {
answer = 0.0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%f%f", &rooms[i].cat, &rooms[i].bean);
rooms[i].rate = rooms[i].cat / rooms[i].bean;
}
sort(rooms, rooms + n, cmp);
for (int i = 0; i < n ; i++) {
if (m > rooms[i].bean) {
answer += rooms[i].cat;
m -= rooms[i].bean;
} else {
answer += (((float) m) / rooms[i].bean) * rooms[i].cat;
break;
}
}
printf("%.3f\n", answer);
}
return 0;
}

Date

2017 年 2 月 28 日

【九度OJ】题目1433:FatMouse 解题报告的更多相关文章

  1. 九度OJ 题目1384:二维数组中的查找

    /********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...

  2. hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人

    钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  3. 九度oj题目&amp;吉大考研11年机试题全解

    九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码).    http://ac.jobdu.com/problem.php?pid=11 ...

  4. 九度oj 题目1007:奥运排序问题

    九度oj 题目1007:奥运排序问题   恢复 题目描述: 按要求,给国家进行排名. 输入:                        有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...

  5. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  6. 九度OJ题目1105:字符串的反码

    tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...

  7. 九度oj题目1009:二叉搜索树

    题目描述: 判断两序列是否为同一二叉搜索树序列 输入:                        开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...

  8. 九度oj题目1002:Grading

    //不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...

  9. 九度OJ题目1003:A+B

    while(cin>>str1>>str2)就行了,多简单,不得不吐槽,九度的OJ真奇葩 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号", ...

  10. 九度oj 题目1024:畅通工程

    题目描述:     省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出了有可能建设公路的若干条道 ...

随机推荐

  1. go定义接口以及类怎么使用接口

    go定义接口以及类怎么使用接口 多态是指代码可以根据类型的具体实现采取不同行为的能力.如果一个类型实现了某个接口,所有使用这个接口的地方,都可以支持这种类型的值. 接口是用来定义行为的类型.这些被定义 ...

  2. 【pheatmap热图scale报错】Error in hclust(d, method = method):NA/NaN/Inf in foreign function call (arg 11)

    初始数据类似如下: 填充下缺失值 data[data==0] <- NA data[is.na(data)] <- min(data,na.rm = T)*0.01 pheatmap(lo ...

  3. 短序列组装Sequence Assembly(转载)

    转载:http://blog.sina.com.cn/s/blog_4af3f0d20100fq5i.html 短序列组装(Sequence assembly)几乎是近年来next-generatio ...

  4. Python基础之数字类型内置方法

    目录 1. 整型内置方法(int) 2. 浮点型内置方法 3. 常用操作 1. 整型内置方法(int) 用途:年龄,号码,等级等 定义: age = 18 常用操作 # 算数运算.比较运算 age = ...

  5. 选择省份、选择省市区举例【c#】【js】

    <style type="text/css"> .labelhide { -webkit-box-shadow: 0px 1px 0px 0px #f3f3f3 !im ...

  6. A Child's History of England.51

    CHAPTER 14 ENGLAND UNDER KING JOHN, CALLED LACKLAND At two-and-thirty years of age, John became King ...

  7. 大数据学习day19-----spark02-------0 零碎知识点(分区,分区和分区器的区别) 1. RDD的使用(RDD的概念,特点,创建rdd的方式以及常见rdd的算子) 2.Spark中的一些重要概念

    0. 零碎概念 (1) 这个有点疑惑,有可能是错误的. (2) 此处就算地址写错了也不会报错,因为此操作只是读取数据的操作(元数据),表示从此地址读取数据但并没有进行读取数据的操作 (3)分区(有时间 ...

  8. [web安全] 利用pearcmd.php从LFI到getshell

    有一段时间没写blog了,主要是事多,加上学的有些迷茫,所以内耗比较大.害,沉下心好好学吧. 漏洞利用背景: 允许文件包含,但session等各种文件包含都已经被过滤了.ctf题中可以关注regist ...

  9. 容器的分类与各种测试(三)——deque

    deque是双端队列,其表象看起来是可以双端扩充,但实际上是通过内存映射管理来营造可以双端扩充的假象,如图所示 比如,用户将最左端的buff用光时,map会自动向左扩充,继续申请并映射一个新的buff ...

  10. java标识接口

    标识接口是没有任何方法和属性的接口,标识接口不对实现类有任何语义上的要求,仅仅表明它的实现类属于一个特定的类型.它非常类似于Web 2.0中的TAG的概念,Java使用它标识某一类对象.主要有两个用途 ...