hdu 1009 FatMouse' Trade
FatMouse' Trade
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 31290 Accepted Submission(s): 10104
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.
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
题意:有 m 的猫粮,和n中交换 f[i] 能换 j[i]
求出能交换得的最大值,
按他们交换比值进行排序
然后计算出最值
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 6 using namespace std; 7 8 #define N 1005 9 10 typedef struct numb 11 { 12 int j,f; 13 double p; 14 numb() 15 { 16 j = 0; 17 f = 0; 18 p = 0.0; 19 } 20 }numb; 21 22 numb a[N]; 23 24 bool fun(numb x,numb y) 25 { 26 if(x.p - y.p > 0.000001) 27 return true; 28 else return false; 29 } 30 int main() 31 { 32 int m,n; 33 while(scanf("%d%d",&m,&n) && n != -1 && m != -1) 34 { 35 int i; 36 for(i = 0; i < n; i++) 37 { 38 scanf("%d%d",&a[i].j,&a[i].f); 39 a[i].p = 1.0*a[i].j/a[i].f; 40 } 41 sort(a,a+n,fun); 42 /*for(i = 0; i < n; i++) 43 printf("%lf ",a[i].p);*/ 44 45 double sum = 0.0; 46 int j; 47 int k = 0; 48 int flag = 0; 49 for(i = 0; i < n; i++) 50 { 51 if(m >= a[i].f) 52 { 53 sum += a[i].j; 54 m -= a[i].f; 55 } 56 else 57 { 58 sum += a[i].p*m; 59 break; 60 } 61 } 62 printf("%0.3lf\n",sum); 63 } 64 return 0; 65 }
yy_room