HDU FatMouse' Trade

FatMouse' Trade

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 69   Accepted Submission(s) : 18
Font: Times New Roman | Verdana | Georgia
Font Size: ← →

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 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
  这题的描述主要是:老鼠有m镑猫粮准备和猫换javebean。现在有n间房子,里面各有若干javebean可供老鼠兑换,兑换的条件是每个房间有各自所需的猫粮。例如5 3 7 2 4 3 5 2这组数据表示老鼠有5镑的猫粮,共有3间房子,每间房子依次有7,4,5镑的javebean各需要2,3,2猫粮换。我们要求的是用5镑换最多的javebean。我们可以知道用2镑换第一间房子的javebean用2镑换第3间房子的javebean,最后用1镑换第二间房子(1*(4/3)的javebean,一共13.333。
AC代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
struct Fun{
    int  CatFood,JavaBean;
    double ratio;
};
Fun t[10000],temp;
bool c(Fun a,Fun b){
   return a.ratio>b.ratio;
}
int main(){
    int m,n,i,j;
    while(scanf("%d%d",&m,&n)&&(m!=-1||n!=-1)){
        i=0;
        while(i<n){
           cin>>t[i].JavaBean>>t[i].CatFood;
            t[i].ratio=(double)t[i].JavaBean/t[i++].CatFood;
        }
        sort(t,t+n,c);
        double sum=0;
        for(i=0;i<n&&m!=0;i++){
                if(m>=t[i].CatFood){
                sum+=t[i].JavaBean;
                m-=t[i].CatFood;
            }
            else {
                sum+=m*((double)t[i].JavaBean/t[i++].CatFood);
                 break;
            }
        }
        printf("%.3f\n",sum);
    }
    return 0;
}

这里用结构体来储存数据,然后调用sort();进行排序计算出最优解。//因为有个参数写错导致一直WA,我找了半天没找到orz,细心啊!
sort用法sort(n,n+m,comp);n是数组名,m是数组长度,comp是自定义函数按照函数规定来进行排序。
运行结果:
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值