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
 

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
struct p
{
double a;
double b;
double c;
};
bool compare(p A,p B)
{
return A.c>B.c;
}
int main()
{
int m,n,i,j;
while(scanf("%d%d",&m,&n)!=EOF)
{
if(m==-1&&n==-1)
break;
p t[1001];
for(i=0;i<n;i++)
{
cin>>t[i].a>>t[i].b;
t[i].c=t[i].a/t[i].b;
}
sort(t,t+n,compare);
double count=0;
for(i=0;i<n;i++)
{
count+=t[i].b;
if(count>m)
break;
}
double sum=0;
//if(i==0)
//printf("%.3lf\n",t[0].c*m);
if(count<m)
{
for(j=0;j<n;j++)
sum+=t[j].a;
printf("%.3lf\n",sum);
}
else
{
for(j=0;j<i;j++)
{
sum+=t[j].a;
m-=t[j].b;
}
printf("%.3lf\n",sum+m*t[i].c);
}
}
return 0;
}
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
int m,n;
int i,j;
double a[1002],b[1002];
double c[1002];
while(scanf("%d%d",&m,&n)!=EOF)
{
if(m==-1&&n==-1)
break;
for(i=0;i<n;i++)
{
cin>>a[i]>>b[i];
c[i]=a[i]/b[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(c[j+1]>c[j])
{
swap(b[j],b[j+1]);
swap(a[j],a[j+1]);
swap(c[j],c[j+1]);
}
}
}
double count=0;
for(i=0;i<n;i++)
{
count+=b[i];
if(count>m)
break;
}
double sum=0;
if(count<m)
{
for(j=0;j<n;j++)
sum+=a[j];
printf("%.3lf\n",sum);
}
else
{
for(j=0;j<i;j++)
{
sum+=a[j];
m-=b[j];
}
printf("%.3lf\n",sum+m*c[i]);
}
}
return 0;
}

A - FatMouse' Trade的更多相关文章

  1. Hdu 1009 FatMouse' Trade

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

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

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

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

  4. 1009 FatMouse' Trade

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

  5. FatMouse' Trade

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

  6. HDU1009 FatMouse' Trade

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

  7. HDU 1009 FatMouse' Trade(贪心)

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

  8. FatMouse' Trade -HZNU寒假集训

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

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

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

  10. 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 ...

随机推荐

  1. Pods 更新后提示Bundle资源找不到

    http://www.oschina.net/question/101347_2159145

  2. java中文乱码解决之道(二)—–字符编码详解:基础知识 + ASCII + GB**

    原文出处:http://cmsblogs.com/?p=1412 在上篇博文(java中文乱码解决之道(一)—–认识字符集)中,LZ简单介绍了主流的字符编码,对各种编码都是点到为止,以下LZ将详细阐述 ...

  3. C#根据汉字生成拼音首字母全称

    static void Main(string[] args) { string s = GetChineseSpell("周杰伦"); Console.WriteLine(s.T ...

  4. lightoj 1104 Birthday Paradox

    题意:给定一个一年的天数,求最少多少人可以使至少两人生日同一天的概率不少于0.5. 用二分去做.检验一个数是否符合时,刚开始实用普通的方法,直接计算,超时了~~,上网搜了一下代码,一位大神使用一个数组 ...

  5. HDU 2685 I won't tell you this is about number theory

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2685 题意:求gcd(a^m - 1, a^n - 1) mod k 思路:gcd(a^m - 1, ...

  6. WIN7/8系统下程序接收不到WM_COPYDATA 消息的原因和解决

    在WIN7/win8,如果发送消息的程序用户权限低于和接收消息的程序,则消 息无法传递.发送程序必须等于或者等于接收程序的权限.如发送与接收 是同一个用户,或者发送是管理员帐户,接收是是普通用户,这样 ...

  7. struts2笔记05-ServletActionContext

    1.ServletActionContext ServletActionContext, 这个类继承自ActionContext, 所以它具有ActionContext的很多功能,不过更重要的是它提供 ...

  8. vb6.0 时间日期

    使用year(now)可以得到4位数的年    你还可以用Format来得到, 还有FormatDateTime 下面两种都是一样的结果:  FormatDateTime(now,vbLongDate ...

  9. Telnet RFC

    http://tools.ietf.org/html/rfc857 http://www.faqs.org/rfcs/rfc854.html 不错: http://blog.csdn.net/chao ...

  10. Silverlight Socket 实现收发信息

    原文 http://www.cnblogs.com/ZetaChow/archive/2009/05/16/2237347.html 刚接触Silverlight的时候,除了其异步应用WCF.流媒体. ...