hdu FatMouse' Trade

来源:互联网 发布:phpstudy配置虚拟域名 编辑:程序博客网 时间:2024/06/05 17:47

Problem Description
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
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.
 

Input
The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1's. All integers are not greater than 1000.
 

Output
For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.
 

Sample Input
5 37 24 35 220 325 1824 1515 10-1 -1
 

Sample Output
13.333

31.500

#include"stdio.h"#include"math.h"int main (){int m,n;//表示我有的猫粮 和房间数 int f[20][2];// 豆 猫粮 int i,j;//计数器 double max; //表示可以得到的最多的豆 double bili[20],bili3;//  猫粮/豆 的比值 int chucunqi1, chucunqi2;//储存器 scanf("%d%d",&m,&n);while(m!=-1&&n!=-1){for(i=0;i<n;i++)             scanf("%d%d",&f[i][0],&f[i][1]);//将每个房间内  豆的的量 和  最多要的猫粮        bili[i]=1.0*f[i][1]/f[i][0];               }      for(i=0;i<(n-1);i++)    {    for(j=0;j<(n-1-i);j++)    {           if(bili[j+1]<bili[j])    {    bili3=bili[j];    bili[j]=bili[j+1];    bili[j+1]=bili3;    chucunqi1=f[j][0];    f[j][0]=f[j+1][0];    f[j+1][0]=chucunqi1;    chucunqi2=f[j][1];     f[j][1]=f[j+1][1];    f[j+1][1]=chucunqi2;    }//一但前者比例值大于后者 立马将数的位置互换      }    }//这里结束后应该会使新一轮的bili比值比从小到大     for(i=0;i<n;i++)    {    for(j=0;j<2;j++)    {    printf("%d ",f[i][j]);}printf("\n");}   for(i=0;i<n;i++)    {    max=0;    m=m-f[i][1];    if(m<=0)    {    for(j=0;j<i;j++)    {    max=max+f[j][0];    }     if(m<0)    {    max=max+(m+f[i][1])*1.0*f[i][0]/f[i][1];   //m=m+f[i][1];       break;    }    if(m=0)    {    max=max+f[i][0];    //m=m+f[i][1];    break;    }    }    } //第二次循环居然没有排序了 还是一开始就没有排序了?     printf("%0.3lf",max);    scanf("%d%d",&m,&n);}return 0;}

原创粉丝点击