天天看点

HDU - 1009 FatMouse' Trade

题目传送门

#include <iostream>
#include <stdio.h>
#include <algorithm>
#define MAXN 1010
using namespace std;
struct Cat
{
    int num;
    int cost;
    double argv;
    }cat[MAXN];
bool com(Cat x, Cat y)
{
    return x.argv > y.argv;
}
int main(void)
{
    int have_coffe;
    int n;
    
    while (scanf("%d%d", &have_coffe, &n)!=EOF)
    {
        if (have_coffe == -1 && n == -1)
            break;
        for (int i = 0; i < n; i++)
        {
            scanf("%d%d", &cat[i].num, &cat[i].cost);
            cat[i].argv = (double)cat[i].num/(double)cat[i].cost;
        }
        sort(cat, cat+n, com);
        int position = 0;
        double sum = 0.0;
        
        while (position < n)
        {//最开始这里我是写为true,因此没有考虑到老鼠的猫食没用完但是猫的咖啡豆已经给完的情况
        //在这里WA了一次
            if (cat[position].cost < have_coffe)
            {
                have_coffe -= cat[position].cost;
                sum += cat[position++].num;
                //如果老鼠的猫食大于这只猫需求的最大量
            }
            else
            {
                sum += cat[position].argv * have_coffe;
                break;//如果猫食不够了能拿多少拿多少
             }
        }
        printf("%.3lf\n", sum);
    }
    
    
    return 0;
}