想按照某个值排序,用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. Android优化——UI检视利器:Hierarchy Viewer

    在Android的SDK工具包中,有很多十分有用的工具,可以帮助程序员开发和测试Android应用程序,大大提高其工作效率.其中的一款叫 Hierachy Viewer的可视化调试工具,可以很方便地在 ...

  2. BZOJ 3572: [Hnoi2014]世界树

    BZOJ 3572: [Hnoi2014]世界树 标签(空格分隔): OI-BZOJ OI-虚数 OI-树形dp OI-倍增 Time Limit: 20 Sec Memory Limit: 512 ...

  3. Eclipse发布地址不同引发的问题

    eclipse发布到workspace metadata时,进不去http://localhost:8888页面.但是,它可以启动JAZZ和“公司的一个工程”. 而 eclipse发布到tomcat ...

  4. 超详细图解:自己架设NuGet服务器

    原文:http://diaosbook.com/Post/2012/12/15/setup-private-nuget-server NuGet 是.NET程序员熟知的给.NET项目自动配置安装lib ...

  5. 【转】【MMX】 基于MMX指令集的程序设计简介

    (一) MMX技术简介 Intel 公司的MMX™(多媒体增强指令集)技术可以大大提高应用程序对二维三维图形和图象的处理能力.Intel MMX技术可用于对大量数据和复杂数组进行的复杂处理,使用MMX ...

  6. 传递消息--第三方开源--EventBus的简单使用

    EventBus下载地址:https://github.com/greenrobot/EventBus MyEvent: package com.zzw.testeventbus; public cl ...

  7. Linux Linux程序练习六

    题目:实现一个so库文件名称为listupper.so,so文件中实现一个函数,函数名为void upper(const char *src, char *desc),调用update后将参数src所 ...

  8. iBatis.net入门指南

    iBatis.net入门指南    - 1 - 什么是iBatis.net ?    - 3 - iBatis.net的原理    - 3 - 新人指路    - 3 - iBatis.net的优缺点 ...

  9. 可以这样去理解group by和聚合函数(转)

    http://www.cnblogs.com/wuguanglei/p/4229938.html 写在前面的话:用了好久group by,今天早上一觉醒来,突然感觉group by好陌生,总有个筋别不 ...

  10. Matlab txt内容替换函数 fgetl fseek

    Data Import and Export  :Low-Level File I/O the contents of the file:    16     5     9     4     2  ...