HDU_1009——老鼠的交易,性价比排序,最大化收益
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.
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 /* 2 算性价比j[i]/f[i],然后按性价比对每个房间的数据重新排序 3 再累加最大化兑换比例的老鼠粮 4 */ 5 #include <cstdio> 6 #include <cstdlib> 7 const int MAX = 1005; 8 /* 9 int compare(const void *a,const void *b) 10 { 11 //return *(double*)a - *(double*)b; //小——大 12 return *(double*)b - *(double*)a; //大——小 13 } 14 */ 15 int main() 16 { 17 int m,n,j[MAX]={0},f[MAX]={0}; 18 while(~scanf("%d%d",&m,&n)) 19 { 20 if(m==-1 && n==-1) 21 break; 22 double j_f[MAX]={0},temp=0; 23 for(int i=1;i<=n;i++) 24 { 25 scanf("%d%d",&j[i],&f[i]); 26 j_f[i]=(double)j[i]/f[i]; 27 } 28 //快速排序:数组首地址,元素个数,一个元素大小,指向比较函数的指针 29 //qsort(j_f+1,n,sizeof(double),compare); 30 //好吧,写到一半发现排序不能这样用- -排序函数还是留着吧- - 31 for(int i=1;i<=n-1;i++) 32 { 33 for(int k=i+1;k<=n;k++) 34 { 35 if(j_f[i]<j_f[k]) 36 { 37 temp=j_f[k]; 38 j_f[k]=j_f[i]; 39 j_f[i]=temp; 40 41 temp=j[k]; 42 j[k]=j[i]; 43 j[i]=(int)temp; 44 45 temp=f[k]; 46 f[k]=f[i]; 47 f[i]=(int)temp; 48 } 49 } 50 } 51 double ans=0; 52 for(int i=0;i<=n;i++) 53 { 54 if(m>=f[i]) 55 { 56 ans+=j[i]; 57 m-=f[i]; 58 } 59 else 60 { 61 ans+=m*j_f[i]; 62 break; 63 } 64 } 65 printf("%.3lf\n",ans); 66 } 67 return 0; 68 }
——现在的努力是为了小时候吹过的牛B!!