FatMouse' Trade
Time Limit : 2000/1000ms(Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 6 AcceptedSubmission(s) : 4
Font:Times New Roman | Verdana | Georgia
Font Size:← →
Problem Description
FatMouseprepared M pounds of cat food, ready to tradewith the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th roomcontains
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, hemay get
J[i]* a% pounds of JavaBeans if he paysF[i]* a% pounds of cat food. Here a is a realnumber. Now he is assigning this homework to you: tell him
the maximum amount of JavaBeans he can obtain.
Input
The inputconsists of multiple test cases. Each test case begins with a line containingtwo non-negative integers M and N. Then N lines follow, each contains two non-negativeintegers J[i] and F[i] respectively. The last test case is followed by two -1's. All integersare not greater than 1000.
Output
For each testcase, print in a single line a real number accurateup to 3(精确到小数点后三位) decimal places, which is the maximum amount of JavaBeansthat 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
Author
CHEN, Yue
Source
ZJCPC2004
//************************* //* 任务介绍:交易 * //* 作者: 何香 * //* 完成时间:2013.10.18 * //************************* #include<iostream> #include<iomanip> using namespace std; int main () { int M,N,i,j;//M是所有的catfood,N是一共有N个room double A[1002];//记录第i个room平均1 food换的javabean的数目 int J[1002],F[1002];//Javabean的数目,catfood的数目 while(cin>>M>>N) { if(M!=(-1)||N!=(-1)) { int t1,t2; double t; //输入每个房里Javabean和food的数目 for(i=1;i<=N;++i) { cin>>J[i]>>F[i]; A[i]=1.0*J[i]/F[i]; } //用冒泡排序法把平均值的大小由大到小排序,同时也使J和F也重新排序 for(i=1;i<N;++i) { for(j=1;j<=N-i;++j) if(A[j]<A[j+1]) { t=A[j],t1=J[j];t2=F[j]; A[j]=A[j+1],J[j]=J[j+1],F[j]=F[j+1]; A[j+1]=t,J[j+1]=t1,F[j+1]=t2; } } int m=0;//记录所加的catfood的数目 int temp; double max_J=0;//记录最大的Javabean数目 //计算达到M时最大的Javabean /* for(i=1;i<=N;++i) { m=temp; m=m+F[i]; if(m<=M) { max_J=max_J+J[i]; } if(m=M) { max_J=max_J+J[i]; break; } else { t=1.0*(M-temp)/F[i]; max_J=max_J+J[i]*t; break; } }*/ for(i=1;i<=N;++i) { if(M>=F[i]) { max_J+=J[i]; M-=F[i]; } else { t=1.0*M/F[i]; max_J+=J[i]*t; M=0; } } cout<<setiosflags(ios::fixed)<<setprecision(3); cout<<max_J<<endl; } else break; } return 0; }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步