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. XmlIgnore的使用

    public class Group { public string GroupName; [XmlIgnore] public string Comments; } 在序列化时,其结果如下所示 &l ...

  2. 小J学python--Exception-异常

    现在我们要打开一个名为fuck的文件,fuck是不存在的,看看异常是怎么工作的 不捕获异常的情况 #打开文件 open('fuck') 执行结果 捕获所有异常 Exception是所有异常类的父类,所 ...

  3. SQL server reporting service - rsReportServerNotActivated error solution

    描述: 今天在原有的数据库版本上增加了Reporting Service 功能,数据库完成安装后,没有发现ReportServer和 ReportServerTemp 两个数据库 于是从其他服务器上, ...

  4. January 05 2017 Week 1st Thursday

    The true nobility is in being superior to your previous self. 真正的高贵在于超越过去的自己. I will be satisfied if ...

  5. HTML 教程

    HTML5 标准 超文本标记语言(英语:HyperText Markup Language,简称:HTML) 是一种用于创建网页的标准标记语言. 您可以使用 HTML 来建立自己的 WEB 站点,HT ...

  6. webstorm中导入git项目

    1.打开webStrom 配置git File–setting

  7. PHP imagechar() 图形验证码 字体太小问题

    bool imagechar ( resource $image , int $font , int $x , int $y , string $c , int$color ) imagechar() ...

  8. python反序列化

    import pickle import os class A(object): def __reduce__(self): a = """python -c 'impo ...

  9. java基础——回文数判断

    /** * 题目描述: * 有这样一类数字,他们顺着看和倒着看是相同的数,例如:121,656,2332等,这样的数字就称为:回文数字.编写一个函数,判断某数字是否是回文数字. * 要求实现方法: * ...

  10. EF中的Guid主键

    除了自增长ID(int),我们还能把主键设置为GUID类型的. 创建我们的数据表 CREATE TABLE dbo.JoinA( AGUID UNIQUEIDENTIFIER PRIMARY KEY ...