• FatMouse' Trade


    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. 

    InputThe 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. 
    OutputFor 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
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstdio>
     4 using namespace std;
     5 struct kang
     6 {
     7     double a, b;
     8 };
     9 bool cmp(const kang&a, const kang&b)
    10 {
    11     return 1.0*a.a / a.b > 1.0*b.a / b.b;
    12 }
    13 int main()
    14 {
    15     kang k[10000];
    16     int m, n;
    17     
    18     while (cin >> m >> n,n == -1 && m == -1)
    19     {
    20         double sum=0;
    21         for (int i = 0; i < n; i++)
    22             cin >> k[i].a >> k[i].b;
    23         sort(k, k + n , cmp);
    24         for (int i = 0; i < n&&m != 0; i++)
    25         {
    26             if (m - k[i].b >= 0)
    27             {
    28                 sum += k[i].a;
    29                 m -= k[i].b;    
    30             }
    31             else
    32             {
    33                 sum += m * 1.0*k[i].a / k[i].b;
    34                 m = 0;
    35             }
    36         }
    37         printf("%.3f
    ", sum);
    38     }
    39     return 0;
    40 }

    简单的贪心题,自定义一个cmp就看解决,不过在用sort时注意区间为左闭右开;

    也可以使用符号重载:

    bool operator < (const kang & a, const kang & b)
    {
    return a.a / a.b > b.a / b.b;
    }(已测试)

  • 相关阅读:
    Java并发编程:同步容器
    poj 1961 Period
    html与JacaScript中的重要思想:预留后路、向后兼容、js分离
    SQL从头開始
    android帧动画,移动位置,缩放,改变透明度等动画解说
    COCOS学习笔记--Cocod2dx内存管理(三)-Coco2d-x内存执行原理
    构建基于Javascript的移动CMS——生成博客(二).路由
    Oracle 单表选择率
    刚接触Joomla,写一下瞎折腾的初感受~
    Android学习笔记之ProgressBar案例分析
  • 原文地址:https://www.cnblogs.com/kangdong/p/8455870.html
Copyright © 2020-2023  润新知