版权声明:本文作者靖心,靖空间地址: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. java8 时间日期操作包总结

  2. css图标与文字对齐实现方法

    1.移动端(安卓设备.ios设备)图标文字垂直居中对齐的最佳常用解决方案是采用弹性盒子布局,可以快捷有效实现子元素未知高度绝对垂直居中对齐.PC端考虑到兼容性的问题,一般不推荐使用弹性盒子,依旧只能采 ...

  3. Linux学习-通过loganalyzer展示MySQL中rsyslog日志

    一.实验环境 系统:CentOS7.6 软件包:apache,php,mariadb-server (都是基于光盘yum源) 源码包:loganalyzer-4.1.7.tar.gz (http:// ...

  4. 【bzoj1398】Vijos1382寻找主人 Necklace

    *题目描述: 给定两个项链的表示,判断他们是否可能是一条项链. *输入: 输入文件只有两行,每行一个由0至9组成的字符串,描述一个项链的表示(保证项链的长度是相等的). *输出: 如果两条项链不可能同 ...

  5. Internet History, Technology, and Security(week8)——Security: Encrypting and Signing

    Hiding Date from Ohters Security Introduction Alice and Bob是密码学.博弈论.物理学等领域中的通用角色之一.Alice(代表A)和Bob(代表 ...

  6. Java数据结构与算法(1):线性表

    线性表是一种简单的数据类型,它是具有相同类型的n个数据元素组成的有限序列.形如如A0,A1,...,An-1.大小为0的表为空表,称Ai后继Ai-1,并称Ai-1前驱Ai. printList打印出表 ...

  7. yum命令查询详解

    一.列举包文件列出资源库中所有可以安装或更新的rpm包# yum list列出资源库中特定的可以安装或更新以及已经安装的rpm包# yum list perl           //列出名为perl ...

  8. Junit单元测试的使用

    这里拿Dynamic Web Project项目来演示,首先创建一个Dynamic Web Project项目,起名,点next, 继续点next, 将web.xml文件勾选,finish, 接下来在 ...

  9. 《上瘾 - 让用户养成使用习惯的四大产品逻辑》 - Nir Eyal, Ryan Hoover

    <上瘾 - 让用户养成使用习惯的四大产品逻辑> - Nir Eyal, Ryan Hoover 前言 1.所谓的消费升级,就是个人愿意付出更高的成本购买与自我价值相匹配的产品.购买即是一种 ...

  10. 读取资源中的GIF文件相应像素宽高度

    代码参考了如下网页的实现: https://www.cnblogs.com/zy791976083/p/9921069.html 整理成一个函数: BOOL GetResGifSize(long nR ...