• HDU-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
     
     
    分析:按J[i]/F[i]的价值从大到小贪。
     
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 const int maxn=1010;
     6 struct javabean{
     7     int j,f;
     8     double v;
     9 }value[maxn];
    10 bool cmp(javabean a,javabean b){
    11     return a.v>b.v;
    12 }
    13 int main()
    14 {
    15     int m,n;
    16     while(scanf("%d%d",&m,&n)){
    17         if(m==-1&&n==-1) break;
    18         for(int i=0;i<n;i++){
    19             scanf("%d%d",&value[i].j,&value[i].f);
    20             value[i].v=(double)value[i].j/(value[i].f*1.0);
    21         }
    22         sort(value,value+n,cmp);
    23         double sum=0.0;
    24         for(int i=0;i<n;i++){
    25             if(m>=value[i].f){
    26                 sum+=value[i].j;
    27                 m-=value[i].f;
    28             }
    29             else{
    30                 sum+=value[i].v*m;
    31                 break;
    32             }
    33         }
    34         printf("%.3f
    ",sum);
    35     }
    36     return 0;
    37 }
  • 相关阅读:
    前端之html的常用标签2和css基本使用
    前端之前端介绍或html的常用标签1
    mysql之练习题4
    mysql之零碎知识
    mysql之索引查询2
    python对象类型----数字&字符串
    初识python---简介,简单的for,while&if
    Shell 语句
    正则表达式----grep
    EG:nginx反向代理两台web服务器,实现负载均衡 所有的web服务共享一台nfs的存储
  • 原文地址:https://www.cnblogs.com/RRirring/p/4711626.html
Copyright © 2020-2023  润新知