1009 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<iostream> 2 #include<stdio.h> 3 using namespace std; 4 int main() 5 { 6 int m,n,x,y; 7 double z; 8 cin>>m>>n; 9 while(m!=-1&&n!=-1) 10 { 11 double s=0; 12 int F[1000],J[10000]; 13 double Q[10000]; 14 for(int i=0;i<n;i++) 15 { 16 cin>>J[i]>>F[i]; 17 if(J[i]==0&&F[i]==0) 18 Q[i]=0; 19 else if(J[i]==0) 20 Q[i]=0; 21 else if(F[i]==0) 22 Q[i]=1000000000; 23 else 24 Q[i]=J[i]/(double)F[i]; 25 } 26 for(int i=0;i<n-1;i++) 27 for(int j=i+1;j<n;j++) 28 { 29 if(Q[i]<Q[j]) 30 { 31 z=Q[i]; 32 Q[i]=Q[j]; 33 Q[j]=z; 34 x=F[i]; 35 F[i]=F[j]; 36 F[j]=x; 37 y=J[i]; 38 J[i]=J[j]; 39 J[j]=y; 40 } 41 }//给F,J,Q全都按照Q排序 42 for(int i=0;i<n&&m!=0;i++) 43 { 44 if(m>F[i]) 45 { 46 s+=J[i]; 47 m=m-F[i]; 48 } 49 else 50 { 51 s+=m*Q[i]; 52 m=0; 53 } 54 }//按照比例计算 55 printf("%.3f\n",s); 56 cin>>m>>n; 57 } 58 return 0; 59 }
浙公网安备 33010602011771号