zoukankan      html  css  js  c++  java
  • HDOJ1009

    #include "iostream"
    #include "algorithm"
    #include "cstdio"
    using namespace std;
    
    struct Point
    {
        int x;
        int y;
        double z;
    };
    
    bool cmp(const Point &a,const Point &b)
    {
        return a.z > b.z;
    }
    int main()
    {
        while(1)
        {
            int M;
            int n;
    
            cin >> M >> n;
    
            if(M == -1 && n == -1)
                break;
            Point *point = new Point[n];
            for(int i=0;i<n;i++)                    //sort
            {
                cin >> point[i].x >> point[i].y ;
                point[i].z = double(point[i].x)/double(point[i].y);
            }
            sort(point,point+n,cmp);
    
            double food = 0;
            int i;
            for(i=0;i<n;i++)                   //caculate
            {
                if(M >= point[i].y)
                {
                    M = M - point[i].y;
                    food = food + point[i].x;
                }
    
                else
                {
                    food = food + double(M) * point[i].x/point[i].y;     //当double与整数相乘(除)就会变成double,完美的解决了精度的问题。遇到精度问题时,一定要把高精度数放在前面。
                    break;
                }
    
            }
            printf("%.3lf
    ",food);
    
        }
        return 0;
    }

    第一:精度问题,要将高精度的放在表达式前面进行优先处理。

    第二:反复记忆上述的指针排序法

    1.用指针new空间

    2.函数传入的指针写法

    3.在结构体内部的东西可以用这种方法排序,比较灵活,可以随意的在结构体内部增加或删减元素

  • 相关阅读:
    CentOS/Ubuntu安装最新的gcc-9
    CentOS 7 源码安装 CMake 3.16.2 最新稳定版。解决 cmake: command not found 问题
    堆排序
    linux可重入、异步信号安全和线程安全
    目录
    教程文档
    推荐文章
    学习文档
    开发工具-索引
    同步/异步/阻塞/非阻塞
  • 原文地址:https://www.cnblogs.com/cunyusup/p/7679694.html
Copyright © 2011-2022 走看看