想按照某个值排序,用sort()函数,结果想了半天不知道用数组怎么解决,然后看了答案,才知道原来可以用struct,想想我真是笨死了。。

原题描述以及答案如下:

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


思路:题意就是给定猫食总量,然后去和猫换JavaBean,每个房间都有一定的JavaBean,每个房间换JavaBean需要的猫食不同。求利用有限的猫食,换取的最多的JavaBean是多少。
注意问题:1.考虑情况要全,比如刚开始就没有考虑foodNeed为0的情况
                  2.当涉及到浮点数运算时要用double,第一次用float没有通过,全部改成double就通过了。

/*Run ID Submit Time Judge Status Pro.ID Exe.Time Exe.Memory Code Len. Language Author
*5849123 2012-04-26 12:44:02 Accepted 1009 78MS 364K 1148 B C++ JackieHowe
*/
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

struct Room
{
int foodHas;
int foodNeed;
double Proportion;
};

bool CompAccordPortion(Room r1,Room r2)
{
if(r1.Proportion==r2.Proportion)
return r1.foodHas>r2.foodHas;
return r1.Proportion>r2.Proportion;
}

int main()
{
int sumFood,numRoom;
while(cin>>sumFood>>numRoom && (sumFood!=-1 || numRoom!=-1))
{
vector<Room> vecRoom;
Room room;
int foodNow=0;
double amount=0;
for(int i=0;i<numRoom;i++)
{
cin>>room.foodHas>>room.foodNeed;
if(room.foodNeed==0)
{
room.Proportion=10000;
}
else
{
room.Proportion=(double)room.foodHas/(double)room.foodNeed;
}
vecRoom.push_back(room);
}
sort(vecRoom.begin(),vecRoom.end(),CompAccordPortion);
for(int i=0;i<vecRoom.size();i++)
{
if((vecRoom[i].foodNeed+foodNow)<=sumFood)
{
amount+=vecRoom[i].foodHas;
foodNow+=vecRoom[i].foodNeed;
}
else
{
amount+=vecRoom[i].Proportion*(double)(sumFood-foodNow);
break;
}
}
cout.precision(3);
cout<<fixed<<amount<<endl;
}
return 0;
}

FatMouse的交易问题的更多相关文章

  1. ZOJ 2109 FatMouse&#39; Trade (背包 dp + 贪婪)

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1109 FatMouse prepared M pounds of cat ...

  2. 题 FatMouse‘Trade

    FatMouse准备了M磅的猫食,准备与守卫仓库的猫交易,这些猫包含他最喜欢的食物,JavaBean. 仓库有N个房间.第i间房间包含J [I]磅的JavaBeans,并且需要F [i]磅的猫粮.Fa ...

  3. HDU-1009的解题报告

    Hdu-1009 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1009 题意:Fatmouse准备M磅的猫食,准备与猫守卫仓库有他最爱吃的食品贸易,J ...

  4. HDU 1009 FatMouse' Trade肥老鼠的交易(AC代码) 贪心法

    题意: 一只老鼠用猫粮来换豆子,每个房间的兑换率不同,所以得尽量从兑换率高的房间先兑换.肥老鼠准备M磅猫粮去跟猫交易,让猫在warehouse中帮他指路,以找到好吃的.warehouse有N个房间,第 ...

  5. 高频交易算法研发心得--MACD指标算法及应用

    凤鸾宝帐景非常,尽是泥金巧样妆. 曲曲远山飞翠色:翩翩舞袖映霞裳. 梨花带雨争娇艳:芍药笼烟骋媚妆. 但得妖娆能举动,取回长乐侍君王. [摘自<封神演义>纣王在女娲宫上香时题的诗] 一首定 ...

  6. Oracle交易流水号问题

    需求:生成交易流水号,每次新增一条记录时都自动加1,且流水号形式为000000001形式的10位数字. 思路:利用序列可以生成自增的流水号,只需要在前面添加N个0即可,同时,由于数字的长度一定,因此可 ...

  7. android/java 根据当前时间判断股票交易状态(未开盘 交易中 休市中 已收盘)

    /** * @param data yyyy-MM-dd HH:mm:ss 时间 * @return 未开盘 交易中 休市中 已收盘 */ public static String getSotckS ...

  8. 微信JSApi支付~订单号和微信交易号

    返回目录 谈谈transactionId和out_trade_no 前一篇微信JSApi支付~坑和如何填坑文章反映不错,所以又写了个后篇,呵呵. 每个第三方在线支付系统中都会有至少两类订单号,其一为支 ...

  9. python 优矿自动化交易

    一.进入官网,打开notebook 自己新建 notebook 二.在代码中编写自己的交易策略 https://uqer.io/help/faqApi/#account相关属性  在帮助文档中可以找到 ...

随机推荐

  1. iOS单例模式(Singleton)写法简析

    单例模式的意思就是只有一个实例.单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例.这个类称为单例类. 1.单例模式的要点: 显然单例模式的要点有三个:一是某个类只能有一个实例: ...

  2. linux测速软件speedtest-cli

    linux测速软件: https://github.com/sivel/speedtest-cli 以下是我在gentoo上测的

  3. shell小结

    一 判断 -d 测试是否为目录.-f 判断是否为文件. -s 判断文件是否为空 如果不为空 则返回0,否则返回1 -e 测试文件或目录是否存在. -r 测试当前用户是否有权限读取. -w 测试当前用户 ...

  4. OAF 中对文字实现html效果及对超级长文本实现默认换行,对只读的messageTextInput中的内容自动换行

    今天遇到一个需求,客户注册页面客户化了一个超级长的注册须知,内容很多.但是样式相对又要做起来好看点. 注册须知的内容使用多个message拼接而成. 老大说rawText支持html样式,于是我想到了 ...

  5. RequiredFieldValidator 根据group组来触发验证

    今天在开发过程中遇到了这样一个问题 在这个用户添加界面中,我使用了多个验证控件RequiredFieldValidator,分别控制用户名密码.在默认情况下,当单击“检查用户名”时,密码的验证控件也被 ...

  6. 客户端禁用cookies后session是否还起效果

    设置session和cookies的代码(webform1.aspx) if (txtName.Text == "wlzcool") { Session["uid&quo ...

  7. 浩瀚先森(guohao1206.com)

    博客搬家啦,新博客地址:浩瀚先森 http://www.guohao1206.com

  8. HoloLens开发手记 - Unity之摄像头篇

    当你穿戴好HoloLens后,你就会处在全息应用世界的中心.当你的项目开启了"Virtual Reality Support"选项并选中了"Windows Hologra ...

  9. openwrt的路由器重置root密码

    家里路由器刷了openwrt,结果长期没登录,忘了root密码. 很容易就找到了这里介绍的办法  http://www.openwrt.org.cn/bbs/thread-12327-1-1.html ...

  10. C#中判断一个数组中是否存在某个数组值 及相关

    声明:reference:http://www.cnblogs.com/icebutterfly/archive/2010/06/22/1762738.html:http://blog.csdn.ne ...