码迷,mamicode.com
首页 > 其他好文 > 详细

杭电 1009(贪心算法)

时间:2014-08-12 17:28:34      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:杭电   算法   贪心   

<span style="font-size:18px;"></span><h1 style="text-align: center; color: rgb(26, 92, 200);">FatMouse' Trade</h1><span size="+0"></span><div style="text-align: center;"><span style="color: green; font-family: Arial; font-size: 12px; "><strong>Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)</strong></span></div><strong></strong><div style="text-align: center;"><span style="color: green; font-family: Arial; font-size: 12px;">Total Submission(s): 43192    Accepted Submission(s): 14432</span></div>

<div class="panel_title" align="left">Problem Description</div><div class="panel_content">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.
</div><div class="panel_bottom"> </div>
<div class="panel_title" align="left">Input</div><div class="panel_content">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.
</div><div class="panel_bottom"> </div>
<div class="panel_title" align="left">Output</div><div class="panel_content">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.
</div><div class="panel_bottom"> </div>
<div class="panel_title" align="left">Sample Input</div><div class="panel_content"><pre><div style="font-family: 'Courier New', Courier, monospace;">5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1</div>
 

Sample Output
13.333 31.500
 

Author
CHEN, Yue
 

Source
思路:直白的贪心算法。

<span style="font-size:18px;">题意:大老鼠,想通过猫粮与猫换取他最喜欢的Java豆,求解所能换取到的Java豆的最大值。</span>
<span style="font-size:18px;">题中的m可以看成钱数,n是仓库数。下面的n行 输入的两个数分别可以看成是数量,需要的金钱数。</span>
<span style="font-size:18px;">代码如下:</span>
<span style="font-size:18px;">#include<stdio.h>
#include<algorithm>
using namespace std;
struct sum{
    double a;
    double b;
    double c;
}s[1010];
int cmp(sum x,sum y)
{
    return x.c>y.c;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&m,&n),!(m==-1&&n==-1))
    {
        int i;
        for(i=0;i<n;i++)
        {
            scanf("%lf%lf",&s[i].a,&s[i].b);
            s[i].c=s[i].a/s[i].b;
        }
        sort(s,s+n,cmp);
        double sum1=0;
        for(i=0;i<n;i++)
        {
            if(m*s[i].c<s[i].a)
            {
                sum1+=m*s[i].c;
                break;
            }
            else
            {
                sum1+=s[i].a;
            }
                m-=s[i].b;
            
        }
        printf("%0.3lf\n",sum1);
        
    }
    return 0;
}</span>
<span style="font-size:18px;">
</span>

杭电 1009(贪心算法),布布扣,bubuko.com

杭电 1009(贪心算法)

标签:杭电   算法   贪心   

原文地址:http://blog.csdn.net/ice_alone/article/details/38514485

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!