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.
代碼實現:
#include<cstdio>
double java[100000],f[100000],a[100000];
int main()
{
double m;
int n;
scanf("%lf%d",&m,&n);
while(m!=EOF&&n!=EOF)
{
double res=0;
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&java[i],&f[i]);
a[i]=java[i]/f[i];
}
for(i=0;i<n;i++)
{
int maxnum=0;
for(int j=1;j<n;j++)
{
if(a[maxnum]<a[j])
maxnum=j;
}
if(m>f[maxnum])
{
res+=java[maxnum];
m-=f[maxnum];
a[maxnum]=-1;
}
else
{
res+=m*a[maxnum];
break;
}
}
printf("%.3lf/n",res);
scanf("%lf%d",&m,&n);
}
return 0;
}