• hdu 1009 FatMouse' Trade


    FatMouse' Trade

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 54549    Accepted Submission(s): 18288


    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
     
    Author
    CHEN, Yue
     
    Source
     
    Recommend
    JGShining   |   We have carefully selected several similar problems for you:  1051 1052 1007 1006 1053 
     
    一道基础的贪心,只需排序就好,运用了sort排序和结构体,感觉十分方便和强大。
     
    题意:老鼠准备了M磅猫食,准备拿这些猫食跟猫交换自己喜欢的食物。有N个房间,每个房间里面都有食物。你可以得到J[i]但你需要付出F[i]的猫食。要你计算你有M磅猫食可以获得最多食物的重量。食物可以被分割。
     
    附上代码:
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 using namespace std;
     6 struct node
     7 {
     8     int a,b;
     9     double c;
    10 }ss[1005];
    11 bool cmp(node x,node y)
    12 {
    13     return x.c>y.c;
    14 }
    15 int main()
    16 {
    17     int n,m,i,j,k,w;
    18     double s;
    19     while(cin>>n>>m)
    20     {
    21         if(n==-1&&m==-1)
    22         break;
    23         s=0;
    24         for(i=0;i<m;i++)
    25         {
    26             cin>>ss[i].a>>ss[i].b;
    27             ss[i].c=(double)ss[i].a/(double)ss[i].b; //求性价比,每元钱可以换多少这种食物
    28         }
    29         sort(ss,ss+m,cmp);  //按性价比大小排序,单价买的越多,性价比越高
    30         for(i=0;i<m;i++)
    31         {
    32             if(n>=ss[i].b)  //拥有的钱超过这个食物全部的价格
    33             {
    34                 s+=ss[i].a;  //全部食物的重量
    35                 n-=ss[i].b;  //减去花费的钱
    36             }
    37             else   //若不够买这种食物的全部
    38             {
    39                 s+=ss[i].c*n;  //花光所有的钱购买这种食物
    40                 break;      //钱为0,跳出循环
    41             }
    42         }
    43         printf("%.3lf
    ",s);  //保存3位小数输出
    44     }
    45     return 0;
    46 }
  • 相关阅读:
    剑指Offer
    剑指Offer
    剑指Offer
    选书
    马的遍历
    从事效应
    魔性的素数环1~20 自带解释~
    [POJ1236]Network of Schools(并查集+floyd,伪强连通分量)
    [HDOJ3974]Assign the task(建树胡搞)
    [HDOJ4027]Can you answer these queries?(线段树,特殊成段更新,成段查询)
  • 原文地址:https://www.cnblogs.com/pshw/p/4760279.html
Copyright © 2020-2023  润新知