贪心思路:按单位猫粮能兑换到的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. 简明Python3教程 19.附录 FLOSS

    FLOSS Free/Libre and Open Source Software, in short, FLOSS is based on the concept of a community, w ...

  2. windows 的使用 —— 注册表(软件的安装和卸载)

    win + r(run):输入 regedit(register edit)进入: 1. 网络连接 比如一些 vpn 安装之后,会对网络连接进行一定的修改,这样在 vpn 工具删除之后,仍然无法消除修 ...

  3. WPF之动画

    原文:WPF之动画 线性关键帧.不连续关键帧动画: <Window.Triggers> <EventTrigger RoutedEvent="Window.Loaded&q ...

  4. Beginner’s Tutorial: 3D Line and Border Effects in XAML

    This mini-tutorial might be for you if you’re having troubles finding the right line colors to achie ...

  5. XF 进度条和指示器

    <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http:/ ...

  6. strlen, wcslen, _mbslen, _mbslen_l, _mbstrlen, _mbstrlen_l, setlocale(LC_CTYPE, "Japanese_Japan")(MSDN的官方示例)

    // crt_strlen.c // Determine the length of a string. For the multi-byte character // example to work ...

  7. delphi自动调整combobox下拉列表宽度(PostMessage CB_SETDROPPEDWIDTH)

    在combobox所在的窗口的Formshow事件里调用即可 procedure SetComboBoxListWidth( AComboBox: TComboBox ); var i: Intege ...

  8. 数据绑定(九)Binding的数据校验

    原文:数据绑定(九)Binding的数据校验 Binding用ValidationRules属性来校验数据的有效性,ValidationRules属性类型是Collection<Validati ...

  9. seajs教程(一):基本用法

    介绍 SeaJS 是一个适用于 Web 浏览器端的模块加载器.使用 SeaJS,可以更好地组织 JavaScript 代码. Sea.js 遵循 CMD 规范,模块化JS代码.依赖的自动加载.配置的简 ...

  10. 使用path制作各类型动画路径

    原文:使用path制作各类型动画路径 <Window x:Class="使用path制作各类型动画路径.MainWindow" xmlns="http://sche ...