zoukankan      html  css  js  c++  java
  • 杭电1009 FatMouse' Trade


    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

    View Code
     1 #include<iostream>
     2 #include<vector>
     3 #include<algorithm>//sort()
     4 #include<iomanip>//格式控制
     5 using namespace std;
     6 
     7 class Cost
     8 {
     9 public:
    10     int JavaBeans;
    11     int cat_food;
    12     double rate;
    13     Cost()
    14     {
    15         JavaBeans = 0;
    16         cat_food = 0;
    17         rate = 0.0;
    18     }
    19     friend istream& operator>>(istream& , Cost&);
    20 };
    21 
    22 inline istream& operator>>(istream& in, Cost& x)
    23     {
    24         //操作符重载
    25         in >> x.JavaBeans >> x.cat_food;
    26         if(in)
    27             x.rate = 1.0*x.JavaBeans/x.cat_food;
    28         return in;
    29     }
    30 
    31 bool compare(Cost a, Cost b)
    32 {
    33     return a.rate > b.rate;
    34 }
    35 
    36 double trade(vector<Cost> vec, int m)
    37 {
    38     double max = 0;
    39     for(int i = 0; i != vec.size(); ++i)
    40         if(m >= vec[i].cat_food)
    41         {
    42             max += vec[i].JavaBeans;
    43             m -= vec[i].cat_food;
    44         }
    45         else
    46         {
    47             max += vec[i].JavaBeans*(1.0*m/vec[i].cat_food);
    48             break;
    49         }
    50     return max;
    51 }
    52 int main()
    53 {
    54     int m, n;
    55     while(cin >> m >> n, m != -1&& n != -1)
    56     {
    57         vector<Cost> vec;
    58         Cost x;
    59         while(n--)
    60         {
    61             cin >> x;
    62             vec.push_back(x);
    63         }
    64         sort(vec.begin(), vec.end(), compare);
    65         cout.precision(3);
    66         cout.setf(ios::fixed);
    67         cout << trade(vec, m) << endl;
    68     }
    69 }
  • 相关阅读:
    C# 反射
    WPF之布局
    java网络编程中的BIO,NIO,AIO
    BIO的阻塞
    AttributeView的用法
    将linux文件中的tab更换为空格的三种方法
    安装nginx-ingress控制器,使用
    docker 搭建jenkins
    Manjaro 18.0.1 系统安装后值得看的两篇博客
    github创建自己的项目并进行推送
  • 原文地址:https://www.cnblogs.com/sanghai/p/2924219.html
Copyright © 2011-2022 走看看