想按照某个值排序,用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. [Erlang37]error/1 exit/1 exit/2 throw/1的区别

    1. error/1 主要是系统用来定义内部错误的: Erlang内建的run time error 一共有10种: function_clause/case_clause/if_clause/bad ...

  2. MTK 平台上如何给 camera 添加一种 preview size

    1,首先检查该项目所使用的是哪一颗sensor, 就以OV2659 为例OV2659 是一颗2M 的摄像头,Sensor 吐出的数据分辨率能达到 1600*1200,肯定是支持 1280*720 的分 ...

  3. SpringMVC视图解析器(转)

    前言 在前一篇博客中讲了SpringMVC的Controller控制器,在这篇博客中将接着介绍一下SpringMVC视图解析器.当我们对SpringMVC控制的资源发起请求时,这些请求都会被Sprin ...

  4. GO To Definition的背后操作【VS2015 C#】

    使用VS开发U3D项目时,去察看某个变量的声明,比如某组件的gameObject变量,会看到如下代码 一看似乎有点晕,这代码什么意思啊,就一个 public GameObject gameObject ...

  5. react native 布局注意点

    一.react native中很多是ES6语法. 1行.表示是js的严格模式. 'use strict';严格模式中变量必须先声明,然后赋值.定义等:还有就是this的绑定. 2行到8行.导入依赖,可 ...

  6. MANIFEST.INF!JAR规范中

    MANIFEST.INF!JAR规范中 META-INF 目录中内容心得.顺带整理了网上资料,提供地址 标签: jarjava产品sunantapache 2012-03-31 17:09 2768人 ...

  7. SVPullToRefresh​ 下拉刷新,上拉加载

    https://github.com/Sephiroth87/ODRefreshControl 类似刷新控件,类似qq动画的那种刷新. 一.下载第三方库 https://github.com/samv ...

  8. Scrum敏捷精要

    本文抽取Scrum中的一些重要思想和概念,对Scrum敏捷执行的主题流程进行精要的介绍. 一.基本思想 个体和互动   高于   流程和工具 工作的软件   高于   详尽的文档 客户合作      ...

  9. android的adb详解(多设备时adb调用)

    在多设备(模拟器)时,想要直接用logcat查看其中一台的状态,或者直接把应用安装到目标设备上时,需要指定设备号.adb devices这个指令可以得到当前设备的序列号(serialNumber).比 ...

  10. iOS 自定义控件开发(上)

    工作需要,最近在进行iOS方面的图表工作.找了很多第三方库都无法实现效果,所以决定自己写一个控件. <iOS 自定义控件开发(上)> <iOS 自定义控件开发(中)> #0 目 ...