zoukankan      html  css  js  c++  java
  • 题目记录6

    题目:http://vj.bit-studio.cn/contest/209424#problem/E

    方法:贪心,有一个巨坑,int x=2,y=3;double z=y/x;y居然会等于1然后搞得我一度怀疑sort有问题,

    代码:

    #include<iostream>
    #include<stdio.h>
    #include<map>
    #include<algorithm>
    using namespace std;
    typedef struct mao
    {
        int j;
        int f;
        double cm;
    }mm;
    bool compare(mm f1, mm f2)
    {
        return f1.cm>f2.cm;
    }
    int main()
    {
        int m, n;
        while ((cin >> m >> n) && m != -1&& n != -1)
        {
            mm *p = new mm[n];
            for (int i = 0; i < n; i++)
            {
                cin >> p[i].j >>p[i].f;
                double x = p[i].j, y = p[i].f;
                p[i].cm = x / y;
                //cout << p[i].cm << endl;
            }
            sort(p,p+n,compare);
            //for (int i = 0; i < n; i++)
            //{
                //for (int j = i + 1; j < n; j++)
                //{
                    //if (p[i].cm < p[j].cm)swap(p[i], p[j]);
                //}
            //}
            double mt=m, nt=0;
            //for(int i=0;i<n;i++)
                //cout << p[i].j << "  " << p[i].f << endl;;
            for (int i = 0; i < n; i++)
            {
                mt -= p[i].f;
                nt += p[i].j;
                if (mt < 0)
                {
                    nt -= p[i].j;
                    double temp = (mt + p[i].f) / p[i].f*p[i].j;
                    nt += temp;
                    break;
                }
            }
            printf("%.3f
    ", nt);
        }
    }
  • 相关阅读:
    super的使用
    Django--自定义 Command 命令
    Django models
    二柱子的编程 四则运算2
    阅读《梦断代码》计划
    随机数计算小学四则运算
    人月神话有感
    软件演化
    软件测试
    软件实现
  • 原文地址:https://www.cnblogs.com/miaos/p/miaoz-1-14.html
Copyright © 2011-2022 走看看