贪心思路:按单位猫粮能兑换到的javaBean从大到小将组合进行排序,总是在当前兑换尽可能多的javabeans

问题描述:点击打开链接 hdoj1009 FatMouse's Trade

源代码:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
struct change
{
int j;
int f;
double g;
}a[1000];
int cmp(change a,change b)
{
return a.g>b.g;
}
int main()
{
int m,n;
int i;
while(cin>>m>>n&&!(n==-1&&m==-1))
{
for(i=0;i<n;i++)
{
cin>>a[i].j>>a[i].f;
a[i].g=a[i].j*1.0/a[i].f;
} sort(a,a+n,cmp);
double maxi=0;
for(i=0;i<n;i++)
{
if(a[i].f<=m)
{
maxi+=a[i].j;
m-=a[i].f;
}
else
{
maxi+=m*a[i].g;
m=0;
}
}
printf("%.3lf\n",maxi);
} return 0;
}

hdoj1009 FatMouse' Trade——贪心算法的更多相关文章

  1. HDU 1009 FatMouse' Trade(贪心)

    FatMouse' Trade Problem Description FatMouse prepared M pounds of cat food, ready to trade with the ...

  2. HDOJ.1009 FatMouse' Trade (贪心)

    FatMouse' Trade 点我挑战题目 题意分析 每组数据,给出有的猫粮m与房间数n,接着有n行,分别是这个房间存放的食物和所需要的猫粮.求这组数据能保证的最大的食物是多少? (可以不完全保证这 ...

  3. ZOJ FatMouse' Trade 贪心

    得之我幸,不得,我命.仅此而已. 学姐说呀,希望下次看到你的时候依然潇洒如故.(笑~) 我就是这么潇洒~哈哈. 感觉大家比我还紧张~ 我很好的.真的 ------------------------- ...

  4. HDOJ-1009 FatMouse' Trade

    http://acm.hdu.edu.cn/showproblem.php?pid=1009 # include <stdio.h> # include <algorithm> ...

  5. FatMouse' Trade (贪心)

    #include <iostream> #include <stdio.h> #include <cstring> #include <cmath> # ...

  6. HDU 1009 FatMouse' Trade (贪心算法)

    题意:就是老鼠要用猫粮换粮食,第i个房间一些东西,要用东西去换,可以不全换.问给定的猫粮最多能换多少粮食. 析:贪心算法.我们先算出来每个房间物品的平均价格是多少,肯定越低越好,并且如果能全换就全换, ...

  7. HDU 1009 FatMouse' Trade(简单贪心 物品可分割的背包问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1009 FatMouse' Trade Time Limit: 2000/1000 MS (Java/O ...

  8. hdu 1009:FatMouse' Trade(贪心)

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  9. HDU 1009 FatMouse' Trade(简单贪心)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1009 FatMouse' Trade Time Limit: 2000/1000 MS (Java/O ...

随机推荐

  1. 像职业选手样编码:地道Python

    Code Like a Pythonista: Idiomatic Python David Goodger goodger@python.org http://python.net/~goodger ...

  2. malloc()与calloc差异

    Both the malloc() and the calloc() functions are used to allocate dynamic memory. Each operates slig ...

  3. asp .net Cookies

    Request.Cookies和Response.Cookies When validating cookies or cookie data from the browser you should ...

  4. window下nodejs爬取gb2312网页出现乱码的解决方案

     发布于 2012-8-22 18:15  5230 次浏览  最后一次编辑是 2013-2-18 22:31 linux环境下,我们可以通过 iconv 这个C++模块来处理Node.JS不支持的字 ...

  5. 【转载】如何使用docker部署c/c++程序

    原文地址:https://blog.csdn.net/len_yue_mo_fu/article/details/80189035 Docker介绍 Docker是一个开源的容器引擎,它有助于更快地交 ...

  6. C/C++使用libcurl库发送http请求(get和post可以用于请求html信息,也可以请求xml和json等串)

    C++要实现http网络连接,需要借助第三方库,libcurl使用起来还是很方便的 环境:win32 + vs2015 如果要在Linux下使用,基本同理 1,下载编译libcurl 下载curl源码 ...

  7. VS2012中使用CEGUI项目发布到XP平台的问题(核心方法就一句话。“你项目使用的所有外部依赖库都用/MT编译。”)

    接着上一篇文章,详细说说如何把一个带CEGUI的项目发布到XP平台. 这个问题纠缠了我好几天.这里把详细解决思路记下来.有同样问题的朋友可以少走很多弯路. 核心方法就一句话.“你项目使用的所有外部依赖 ...

  8. 如果你说最近在看《诛仙》,平时喜欢玩LOL,你就是在把自己往悬崖上推

    面试官可能会问你一些和技术看上去没有任何关系的问题,比如问你最近在看什么书,学习之余喜欢做什么,常去哪些网站之类的.如果你说最近在看<诛仙>,平时喜欢玩LOL,你就是在把自己往悬崖上推.实 ...

  9. Qt MVC设计模式(五篇)

    http://blog.csdn.net/qq_19672579/article/details/46952675http://blog.csdn.net/qq_19672579/article/de ...

  10. WPF TreeView HierarchicalDataTemplate

    原文 WPF TreeView HierarchicalDataTemplate HierarchicalDataTemplate 的DataType是本层的绑定,而ItemsSource是绑定下层的 ...