zoukankan      html  css  js  c++  java
  • HDU1009 贪心水题

    题目:HDU1009

    每个房间里的J除以F得到比例,按照比例排序,拿食物去比例较大的房间换

    #include <iostream>
    #include <algorithm>
    //#include <fstream>
    #include <iomanip>
    #define FOR(a,b) for(int i = (a);i < (b);i ++)
    using namespace std;
    
    struct Node
    {
        double j,f;
        double proportion;
        bool operator<(Node& n)
        {
            return proportion > n.proportion;
        }
    }N[1008];
    
    
    
    int main()
    {
        //ifstream cin("in.txt");
        int n;
        double m;
        double ans;
        while(cin>>m>>n,m != -1)
        {
            memset(N,0,sizeof(Node)*n);
            ans = 0;
            FOR(0,n)
            {
                cin>>N[i].j>>N[i].f;
                N[i].proportion = N[i].j / N[i].f;
            }
            sort(N,N+n);
            FOR(0,n)
            {
                if(N[i].f <= m)
                {
                    m -= N[i].f;
                    ans += N[i].j;
                }else if(m <= 0){
                    break;
                }else{
                    ans += m * N[i].proportion;
                    break;
                }
            }
            cout<<fixed<<setprecision(3)<<ans<<endl;
        }
    }
    本博客所有博文,若无专门说明皆为原创,转载请注明作者和出处!
  • 相关阅读:
    关于XML文档
    Why sql is called structured query language?1
    UML学习---交互
    C#为什么不采用多继承:
    url中
    array
    hard
    构造函数返回值
    布局容器layout Container
    k8s的概念
  • 原文地址:https://www.cnblogs.com/ifinver/p/2706465.html
Copyright © 2011-2022 走看看