FatMouse' Trade
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
1 #include <stdio.h> 2 #define N 1001 3 4 int main(){ 5 int m; 6 int n; 7 int i; 8 int j; 9 int J[N]; 10 int F[N]; 11 double unit[N]; 12 double temp; 13 double get; 14 15 while(1){ 16 scanf("%d%d",&m,&n); 17 18 if(m==-1 && n==-1) 19 break; 20 21 get=0; 22 for(i=0;i<n;i++){ 23 scanf("%d%d",&J[i],&F[i]); 24 unit[i]=(double)J[i]/F[i]; 25 } 26 27 for(i=0;i<n-1;i++){ 28 for(j=i+1;j<n;j++){ 29 if(unit[i]<unit[j]){ 30 temp=unit[i]; 31 unit[i]=unit[j]; 32 unit[j]=temp; 33 34 temp=J[i]; 35 J[i]=J[j]; 36 J[j]=temp; 37 38 temp=F[i]; 39 F[i]=F[j]; 40 F[j]=temp; 41 } 42 } 43 } 44 45 46 for(i=0;i<n;i++){ 47 if(m>=F[i]){ 48 m=m-F[i]; 49 get+=J[i]; //这里J[i]改成unit[i]*F[i]会丢失精度导致出错 50 51 if(m==0) 52 break; 53 } 54 55 else{ 56 get+=unit[i]*m; 57 58 break; 59 } 60 } 61 printf("%.3lf\n",get); 62 } 63 return 0; 64 }