标签:
1 #include <stdio.h> 2 #include <algorithm> 3 struct room{int J,F;double P;}a[1010]; 4 bool cmp(const room&a, const room&b){return a.P>b.P;} 5 6 int main() 7 { 8 int M, N; 9 while(scanf("%d%d",&M,&N)&&~M) { 10 for(int i=0; i<N; i++) { 11 scanf("%d%d", &a[i].J, &a[i].F); 12 a[i].P=(double)a[i].J/a[i].F; 13 } 14 std::sort(a,a+N,cmp); 15 double maxf=.0; 16 for(int i=0; i<N; i++) 17 if(M>a[i].F) { 18 M-=a[i].F; 19 maxf+=a[i].J; 20 }else{ 21 maxf+=(double)a[i].J * M /a[i].F; 22 break; 23 } 24 printf("%.3f\n",maxf); 25 } 26 return 0; 27 }
HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
标签:
原文地址:http://www.cnblogs.com/BlackStorm/p/4940846.html