HDU-1009的解题报告

Hdu-1009

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1009

题意:Fatmouse准备M磅的猫食,准备与猫守卫仓库有他最爱吃的食品贸易,JavaBean仓库有N个房间。第i个房间包含J [i]磅 JavaBeans和需要F [i]磅猫食。FatMouse并不交易房间里的所有JavaBeans相反,他可以得到J [i] *a%磅的JavaBeans如果他支付F [i] *a%磅的猫食,这里a是实数。他现在是分配给你这个作业:告诉他,他能获得的最多数量的JavaBeans。

思路:(1)定义一个结构体,用于存放J(i)和F(i),同时存放每个房间的交换比率a%;(2)输入M,N,J(i),F(i);(3)将每个房间的交换比率从大到小进行排序;(4)求出最多能得到的JavaBeans。

#include<stdio.h>

typedef struct

{

         int j,f;

         double r;

}node;

node a[1000],t;///////////////////注意此处应在外部定义;

int main()

{

         int i,k,m,n;

         double sum;

         while(~scanf("%d%d",&m,&n)&&(m!=-1||n!=-1))

         {

                   sum=0;

                   for(i=0;i<n;i++)

                   {

                            scanf("%d%d",&a[i].j,&a[i].f);

                            a[i].r=(double)a[i].j/a[i].f;////////求出每个房间的交换比率

                   }

//////////////////////////////////////////////将每个房间的交换比率按照降序排列

                   for(i=0;i<n-1;i++)

                   {

                            for(k=i+1;k<n;k++)

                            {

                                     if(a[i].r<a[k].r)

                                     {

                                               t=a[i];

                                               a[i]=a[k];

                                               a[k]=t;

                                     }

                            }

                   }

//////////////////////////////////////////求出总和

                   for(i=0;i<n;i++)

                   {

                            if(m>=a[i].f)

                            {

                                     sum=sum+a[i].j;

                                     m=m-a[i].f;

                            }

                            else

                            {

                                     sum=sum+m*a[i].r;

                                     m=0;

                                     break;

                            }

                   }

                   printf("%.3lf\n",sum);

         }

         return 0;

}

posted @ 2018-04-07 15:11  里昂静  阅读(163)  评论(0编辑  收藏  举报