FatMouse' Trade

/*
problem: FatMouse' Trade
this is greedy problem.
firstly:we should calculate the average  J[i]/F[i] and sort it
secondly: according to the m and choose the most high ones
*/
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

struct cell
{
    double j;
    double f;
    double rate;
};
bool cmp(cell x,cell y)
{
    return x.rate > y.rate;
}
int main()
{
    int m,n;
    cell temp;
    vector<cell> vec;
    while (scanf("%d%d", &m, &n) != EOF)
    {
        if ((m == -1)&&(n == -1))
            break;
        double count = 0.0;
        while (n--)
        {
            cin>>temp.j>>temp.f;
            if (temp.f != 0)
            {
                temp.rate = (double)temp.j/temp.f;
                vec.push_back(temp);
            }
            else
            {
                count += temp.j;
            }

        }
        sort(vec.begin(), vec.end(), cmp);
        for (int i = 0; (i < vec.size())&&(m > 0); i++)
        {
            if (m-vec[i].f >= 0)
            {
                count += vec[i].j;
                m -= vec[i].f;
            }
            else
            {
                //count += (double)((double)(m/vec[i].f))*vec[i].j;
                double temp1 = (double)m/vec[i].f;
                count += temp1*vec[i].j;
                m = 0;
            }
        }
        vec.clear();
        printf("%.3f
",count);
    }
}
/*此题除了要满足例子以外,还要满足一些条件才能真正算ac:
0 1
1 0
1.000

1 0
0.000

5 4
10000 5
2000 2
100 0
300 0
10400.000

数据类型用double,就这样*/
原文地址:https://www.cnblogs.com/maverick-fu/p/3961680.html