FatMouse' Trade
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 68716 Accepted Submission(s): 23432
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.
5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1
13.333 31.500
题目代码:
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; struct infor { double value; double cost; double aver; }a[1004]; bool cmp(infor p1,infor p2) { return p1.aver < p2.aver; } int main() { int M,N;//M磅猫粮,N个房间 double _max; while(~scanf("%d%d",&M,&N)) { if(M < 0 && N < 0) break; for(int i = 0; i < N; i++) { scanf("%lf%lf",&a[i].value,&a[i].cost); a[i].aver = a[i].cost/a[i].value; } sort(a,a+N,cmp);//按单位价格进行排序 _max = 0; for(int i = 0; i < N; i++) { if(M >= a[i].cost) { _max += a[i].value; M = M - a[i].cost; } else { _max += (M*a[i].value)/a[i].cost; break; } } printf("%.3lf\n",_max); } return 0; }