HDU--1009 -- FatMouse' Trade
2013-07-26 16:58
281 查看
FatMouse' Trade
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 33514 Accepted Submission(s): 10902
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.
[align=left]Output[/align]
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
Code:
#include"cstdio" #include"cstdlib" #include"algorithm" #include"iostream" using namespace std; typedef struct changeinfo { int j; int f; }changeinfo; //房间信息的结构体 bool cmp(changeinfo a,changeinfo b) { return 1.0*a.j/a.f > 1.0*b.j/b.f;//乘以1.0使之变为double型 } int main() { int m,n,i,k; double sum; changeinfo a[1001]; while(scanf("%d%d",&m,&n),(m+1)&&(n+1)) { sum=0; for(i=0;i<n;i++) scanf("%d%d",&a[i].j,&a[i].f); sort(a,a+n,cmp); //根据相除结果从大到小排序,参数依次为参与排序的首尾地址和cmp比较函数 //for(i=0;i<n;i++) // printf("%d %d\n",a[i].j,a[i].f); if(m==0)//==!题目的问题需要考虑这两种情况才能AC { for(i=0;i<n;i++) if(a[i].f==0) sum = sum + a[i].j; } if(n==0) sum = 0; if(m!=0 && n!=0) { for(i=0,k=m;i<n,k>=0;i++) { if(a[i].f>k) //剩余猫粮不足的情况 sum = (1.0*a[i].j/a[i].f)*k + sum; else sum = a[i].j + sum; k = k - a[i].f; } } printf("%.3lf\n",sum); } return 0; }
相关文章推荐
- HDU 1009 FatMouse' Trade(贪心)
- HDU1009 FatMouse' Trade
- HDU 1009 FatMouse' Trade
- hdu 1009 FatMouse' Trade
- HDU's ACM 1009 FatMouse' Trade
- hdu 1009 FatMouse' Trade(贪心)
- hdu-1009-贪心-FatMouse' Trade
- hdu1009 nyoj824 FatMouse' Trade 简单贪心
- HDU 1009 FatMouse' Trade(贪心)
- HDU 1009 FatMouse' Trade
- hdu-oj 1009 FatMouse' Trade
- HDU 1009 FatMouse' Trade (贪心)
- HDU 1009 FatMouse' Trade
- HDU1009 FatMouse' Trade 【贪心】
- HDU - 1009 FatMouse' Trade 贪心
- hdu-1009 FatMouse' Trade 贪心算法
- HDU 1009 (FatMouse' Trade)
- HDU 1009 FatMouse' Trade
- HDU 1009 FatMouse' Trade
- HDU1009 FatMouse' Trade 贪心