版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/。未经本作者同意不得转载。 https://blog.csdn.net/kenden23/article/details/31418535

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

贪心法水题,

个人认为这句话难理解:he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food

这样表达能够购买几分之几的。

#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
struct twoInts
{
int j, f;
bool operator<(const twoInts two) const
{
double a = (double)j / (double)f;
double b = (double)two.j / (double)two.f;
return a > b;
}
}; int main()
{
int M, N;
while (scanf("%d %d", &M, &N) && -1 != M)
{
vector<twoInts> vt(N);
for (int i = 0; i < N; i++)
{
scanf("%d", &vt[i].j);
scanf("%d", &vt[i].f);
}
sort(vt.begin(), vt.end());
double maxBean = 0.0;
for (int i = 0; i < N; i++)
{
if (M >= vt[i].f)
{
maxBean += vt[i].j;
M -= vt[i].f;
}
else
{
maxBean += (double)vt[i].j * M / (double)vt[i].f;
break;
}
}
printf("%.3lf\n", maxBean);
}
return 0;
}

HDU 1009 FatMouse' Trade题解的更多相关文章

  1. HDU 1009 FatMouse' Trade(简单贪心)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1009 FatMouse' Trade Time Limit: 2000/1000 MS (Java/O ...

  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

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

  4. hdu 1009:FatMouse' Trade(贪心)

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

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

  6. HDU 1009 FatMouse' Trade(贪心)

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

  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【贪心】

    解题思路:一只老鼠共有m的猫粮,给出n个房间,每一间房间可以用f[i]的猫粮换取w[i]的豆,问老鼠最多能够获得豆的数量 sum 即每一间房间的豆的单价为v[i]=f[i]/w[i],要想买到最多的豆 ...

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

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

随机推荐

  1. java IO操作分类

  2. 【LuoguP5171】Earthquake

    题目链接 题意 求满足如下不等式的非负整数 \(x,y\) 的对数 \[ax+by\leq c\] Sol a,b,c 都是非负的,那么先随便变个形: \[y\leq\frac{c-ax}{b}\] ...

  3. linux-LVM磁盘扩容

    查看磁盘 [ops@stock_kline_database ~]$ sudo fdisk -l 磁盘 /dev/sda: 字节, 个扇区 Units = 扇区 of * = bytes 扇区大小(逻 ...

  4. 【leetcode】1161. Maximum Level Sum of a Binary Tree

    题目如下: Given the root of a binary tree, the level of its root is 1, the level of its children is 2, a ...

  5. 大数阶乘(N! Plus)问题

    解题思路 将正整数N从1到N逐位相乘,即1 * 2 * 3...... * (N-1) * N.每次相乘后的值会存储到array[]中,其中一个数组元素存储值中的一位数.当值小于10时直接存储,值大于 ...

  6. cryto-js 常用加密库 md5加密

    安装 npm i crypto-js 使用 import CryptoJs from 'crypto-js' CryptoJs.MD5(password).toString() password 会被 ...

  7. android 如何引用jar包

    首先,把jar包放到项目目录app/libs下,然后是项目引用:三个方法 方法一.添加compile 打开app下的build.gradle,在dependencies里面添加 implementat ...

  8. (3.3)狄泰软件学院C++课程学习剖析四

    对课程前面40课的详细回顾分析(二) 1.一个类的成员变量是对于每个对象专有的,但是成员函数是共享的. 2.构造函数只是决定一个对象的初始化状态,而不能决定对象的诞生.二阶构造人为的将初始化过程分为了 ...

  9. css内容过长显示省略号的几种解决方法

    单行文本(方法一): 语法: text-overflow : clip | ellipsis 参数: clip : 不显示省略标记(...),而是简单的裁切 (clip这个参数是不常用的!) elli ...

  10. [CSP-S模拟测试]:集合论(模拟)

    题目传送门(内部题73) 输入格式 输入文件$jihe.in$ 第一行一个整数$m$,表示操作的次数. 接下来$m$行,每行描述一个操作. 每行的开始都是一个数字,$1,2,3,4$依次代表$unio ...