【ACM】HDOJ 1009 FatMouse' Trade

HDOJ 1009

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.

题目描述:
胖老鼠准备了M榜的猫食,想要用来与守卫藏有他最爱的JavaBean的仓库的猫猫们进行交易。
仓库有N个房间,第i个房间里面有J[i]榜的JavaBean,同时需要F[i]榜的猫食才能换取。
胖老鼠可以只按比例换取JavaBean而不是换取整个房间的JavaBean。
现在它把交易计划交给你来处理:告诉它,它最多能得到多少榜JavaBean。

输入说明:
允许多次案例输入,每个案例的第一行为两个无符号整数 M和N。
接着是N行输入,每一行包含无符号整数J[i]和F[i]。
程序输入以两个-1结束。
所有的整数都不会超过1000。

输出说明:
对于每个案例,输出一个保留三位有效数字的实数,用来表示这个案例中胖老鼠最大能获得的JavaBean。

我的思路
要使JavaBean最大,那就得要每一榜猫食换取尽可能多的JavaBean。
所以 先计算出每一个房间的 单位食物能换取的JavaBean,即 V[i] = J[i]/F[i]。
接着 对数组V排序,V[i]越大,猫食用的越值得。
剩余的猫食小到不足以换取整个房间的JavaBean时,选择按比例换取JavaBean。
#include <iostream>
#include <iomanip>
//#include <fstream>
#include <cstdlib>
#include<algorithm>

using namespace std;
struct s_ROOM
{
    double JB;
    double food;
    double value;

}Rooms[1000];
double GetJavaBean(int);
bool cmpValue(s_ROOM,s_ROOM);

int M,N;
int main()
{
 //   ifstream input("F:\input.txt");

    while(cin>>M>>N && M!=-1 && N!=-1)
    {

        for(int i=0;i<N;i++)
        {
            cin>>Rooms[i].JB>>Rooms[i].food;
            Rooms[i].value  = Rooms[i].JB/Rooms[i].food;
        }
        sort(Rooms,Rooms+N,cmpValue);
        cout<<fixed;
        cout<<setprecision (3)<<GetJavaBean(M)<<endl;
    }
    return 0;
}

double GetJavaBean(int totalFood)
{
    double ans=0;
    int leftFood = totalFood;
    int i=0;
    for(i=0;i<N && leftFood>= Rooms[i].food;i++)
    {
    //    cout<<Rooms[i].JB<<','<<Rooms[i].food<<','<<Rooms[i].value<<endl;
        ans+=Rooms[i].JB;
        leftFood-=Rooms[i].food;
    }
    if(i<N)
    {
      //  cout<< Rooms[i].JB*leftFood/Rooms[i].food<<endl;
        ans+=Rooms[i].JB*leftFood/Rooms[i].food;
    }



    return ans;
}

bool cmpValue(s_ROOM r1,s_ROOM r2)
{

    return r1.value-r2.value >0;
}

其中用到了STL(标准模板库)中的sort函数用来对value进行大到小排序。
AC要点:实数类型选择double!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值