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
-----------------------------------------------------------------------------------------------------
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#define SIZE 1001
int J[SIZE];
int F[SIZE];
double rate[SIZE];
int bigger(int a, double keyRate, double keyJ);
int main()
{
int M, N;
int i, j, tempIndex;
double tempRate,tempJ,tempF;
double total;
while (scanf("%d%d", &M, &N) == 2)
{
total = 0;
if (M == -1 && N == -1)
break;
for (i = 0; i < N; i++)
{
scanf("%d%d", &J[i], &F[i]);
rate[i] = ((double)F[i] / J[i]);
}
//插入排序
for (i = 1; i < N; i++)
{
tempRate = rate[i];
tempJ = J[i];
tempF = F[i];
tempIndex = i;
j = i - 1;
while (j >= 0 && bigger(j, tempRate, tempJ))
{
rate[j + 1] = rate[j];
J[j + 1] = J[j];
F[j + 1] = F[j];
j = j - 1;
}
rate[j + 1] = tempRate;
J[j + 1] = tempJ;
F[j + 1] = tempF;
} for (i = 0; i < N; i++)
{
//判断比率为0,也就是免费送的情况
if (M > F[i] || rate[i] <= 0.0000001)
{
total += J[i];
M -= F[i];
}
else
{
total += (double)M / F[i] * J[i];
break;
}
}
printf("%0.3f\n", total);
}
} int bigger(int a, double keyRate, double keyJ)
{
if (fabs(rate[a] - keyRate) <= 0.000001)
return J[a] < keyJ;
else
{
return rate[a] > keyRate;
}
}

  

ACM1009:FatMouse' Trade的更多相关文章

  1. HDU1009:FatMouse' Trade(初探贪心,wait)

    FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containi ...

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

  3. FatMouse' Trade(杭电ACM---1009)

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

  4. HDU 1009:FatMouse&#39; Trade(简单贪心)

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

  5. Hdu 1009 FatMouse' Trade

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

  6. 题目1433:FatMouse (未解决)

    题目描述: FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse co ...

  7. FatMouse' Trade

    /* problem: FatMouse' Trade this is greedy problem. firstly:we should calculate the average J[i]/F[i ...

  8. HDU1009 FatMouse' Trade

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

  9. FatMouse' Trade -HZNU寒假集训

    FatMouse' Trade FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the wa ...

随机推荐

  1. webpack笔记三 管理输出

    webpack笔记三 管理输出 增加src/print.js: export default function printMe() { console.log('I get called from p ...

  2. Win10开启PIN码使用教程

    很多电脑爱好者对于Win10内置的PIN码功能不太了解,很多朋友都还没有使用.其实,创建PIN码可以提到密码使用,当你登录到Windows和其它应用服务时,可以通过PIN码替代输入账户密码,提升安全性 ...

  3. 沉淀再出发:java中的equals()辨析

    沉淀再出发:java中的equals()辨析 一.前言 关于java中的equals,我们可能非常奇怪,在Object中定义了这个函数,其他的很多类中都重载了它,导致了我们对于辨析其中的内涵有了混淆, ...

  4. namespace 相关

    #include <iostream>using namespace std;namespace first{ int var = 5;}namespace second{ double ...

  5. HBase编程 API入门系列之create(管理端而言)(8)

    大家,若是看过我前期的这篇博客的话,则 HBase编程 API入门系列之put(客户端而言)(1) 就知道,在这篇博文里,我是在HBase Shell里创建HBase表的. 这里,我带领大家,学习更高 ...

  6. rsync 服务器配置过程

    rsync的原理和相关算法不赘述,资料很多 1.准备两台机器并确保都已经安装rsync a机器:192.168.1.150 ,用作客户端测试 b机器:192.168.1.151用作server端 先介 ...

  7. MVC中使用过滤器做权限认证需要注意的地方

    最近一项目接近尾声,正准备调试的时候发现一条原本不应该执行的SQL语句执行报错了,本就不应该执行,所以当然也就出错,找了一下,问题如下. 我这里是直接重写OnActionExecuting方法来达到目 ...

  8. #npm install# MSBUILD : error MSB4132: 无法识别工具版本“2.0”。可用的工具版本为 "4.0"。

    0.问题描述 Windows 10 最近使用npm install安装项目依赖包,当自动执行至node-gyp rebuild时报错: C:\Users\dsl\Desktop\Pros\ant-de ...

  9. (六)Linux下的压缩命令

    ======================================================================================== .zip格式的压缩和解 ...

  10. 使用tidylib解决不规则网页问题

    windows 下安装tidylib 先去https://pypi.python.org/pypi/pytidylib 下载pytidylib 跑官方提供的测试用例报错 ition 0: unexpe ...