• hdu 1009 贪心入门


    Mycode:

    compile error,i don't know why?

    #include <iostream>
    #include<algorithm>
    #include<cstdio>
    #define maxn 10005
    using namespace std;
    struct room
    {
        int j;
        int f;
        double val;
    };
    struct room r[3001];
    bool compare(struct room a,struct room b)
    {
      return a.val > b.val;
    }
    int main()
    {
        double n,m;
        while(cin >> m >> n)
        {
            if(m == -1&&n == -1)
                break;
            for(int i = 0;i < n;i ++)
            {
                cin >> r[i].j;
                cin >> r[i].f;
                r[i].val = (double)r[i].j/r[i].f;
            }
            struct room *p = &p[0];
            sort(p,p+n,compare);
            double ans = 0;
            for(int i = 0;i < n&&m > 0;i ++)
            {
                if(m > r[i].f){
                    ans += r[i].j;
                    m -= r[i].f;
                }
                else{
                    ans += r[i].j * (m/r[i].f);
                    m = 0;
                }
            }
            printf("%.3f
    ",ans);
        }
        return 0;
    }

    Correct code:

    #include<stdio.h>
    #include<algorithm>
    using namespace std;
    struct Trade
    {
        int j,f;
        double percent;
    }mouse[3001];
    bool cmp(Trade a,Trade b)
    {
        return a.percent>b.percent;
    }
    int main()
    {
        int n,m;
        while(scanf("%d%d",&m,&n)!=EOF&&(n!=-1||m!=-1))
        {
            int i;
    
            for(i=0;i<n;i++)
            {
                scanf("%d%d",&mouse[i].j,&mouse[i].f);
                mouse[i].percent=(double)mouse[i].j/mouse[i].f;
            }
            sort(mouse,mouse+n,cmp);
            double sum=0;
            for(i=0;i<n;i++)
            {
                if(m>mouse[i].f)
                     {
                         sum+=mouse[i].j;
                         m-=mouse[i].f;
                     }
                else
                    //break;
                {
                    sum+=mouse[i].percent*m;
                    m=0;
                    break;
                }
    
    
            }
    
            printf("%.3lf
    ",sum);
    
        }
        return 0;
    }
  • 相关阅读:
    gitLab、docker
    Spring源码分析
    Tomcat堆内存分析
    Kafka入门一
    Java NIO
    spring注解
    websocket即时通讯
    pycharm安装dlib库
    python+opencv人脸识别是否戴口罩
    2021年暑假周总结1
  • 原文地址:https://www.cnblogs.com/zhangjialu2015/p/5296840.html
Copyright © 2020-2023  润新知