HDU1009
1 #include<stdio.h> 2 #include<stdlib.h> 3 struct gg 4 { 5 int j; 6 int f; 7 double p; 8 }gl[1005],tp; 9 int cmp(const void *a,const void *b) 10 { 11 return (*(struct gg*)b).p>(*(struct gg*)a).p?1:-1; 12 } 13 int main() 14 { 15 int m,n,i; 16 double left,sum; 17 while(scanf("%d%d",&m,&n)==2&&(m!=-1)&&(n!=-1)) 18 { 19 for(i=0;i<n;i++) 20 { 21 scanf("%d%d",&gl[i].j,&gl[i].f); 22 gl[i].p=1.0*gl[i].j/gl[i].f; 23 } 24 qsort(gl,n,sizeof(gl[0]),cmp); 25 left=m*1.0; 26 sum=0.0; 27 for(i=0;i<n&&left>=0;i++) 28 { 29 if(left>=gl[i].f) 30 { 31 sum=sum+1.0*gl[i].j; 32 left=left-1.0*gl[i].f; 33 //if(left==0)break; 34 } 35 else 36 if(left>=0&&left<gl[i].f) 37 { 38 sum=sum+(gl[i].p)*left; 39 left=left-(gl[i].p)*left; 40 //left=0; 41 //break; 42 } 43 } 44 printf("%.3lf\n",sum); 45 } 46 return 0; 47 }
keep moving...