首页 > 代码库 > HDOJ 1070 Milk

HDOJ 1070 Milk

【题意】喝牛奶,

1. 每天喝200ml。

2. 最多喝5天,超过5天剩下多少也不喝了。

3. 剩下的如果小于一天喝的(200ml)就不要了。 

4. 所有的牛奶都是当天产的。

5. 按照每天的价格选择最便宜的。

6. 如果价格都是最便宜的,选择大瓶(容量大)的。

【代码:WA】:考虑错了2,5,6条件。

#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;

#define MAX 100+10
struct Milk
{
    char name[MAX];
    int price;
    int volumn;
};

int cmp(const void *a, const void *b)
{
    struct Milk *c = (Milk*)a;
    struct Milk *d = (Milk*)b;
    if ((c->price/c->volumn) == (d->price/d->volumn))
        return strlen(c->name) - strlen(d->name);
    else
        return (c->volumn/c->price) - (d->volumn/d->price);
}

int main()
{
    int N = 0;
    cin >> N;
    while (N--)
    {
        Milk milk[MAX];
        int n = 0, i = 0;
        cin >> n;
        for (i = 0; i < n; )
        {
            Milk temp;
            cin >> temp.name >> temp.price >> temp.volumn;
            if (temp.volumn >= 200)
                milk[i++] = temp;
            else
                n--;
        }
        qsort(milk, n, sizeof(milk[i]), cmp);
        cout << milk[n-1].name << endl;
    }
    return 0;
}

【代码:AC】

#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;

#define MAX 100+10
struct Milk
{
    char name[MAX];
    int price;
    int volumn;
};

int cmp(const void *a, const void *b)
{
    struct Milk *c = (Milk*)a;
    struct Milk *d = (Milk*)b;
    int day1 = c->volumn/200;
    if (day1 >5)
        day1 = 5;
    int day2 = d->volumn/200;
    if (day2 >5)
        day2 = 5;
    if ((c->price/day1) == (d->price/day2))
        return c->volumn - d->volumn;
    else
        return (d->price/day2) - (c->price/day1);
}

int main()
{
    int N = 0;
    cin >> N;
    while (N--)
    {
        Milk milk[MAX];
        int n = 0, i = 0;
        cin >> n;
        for (i = 0; i < n; )
        {
            Milk temp;
            cin >> temp.name >> temp.price >> temp.volumn;
            if (temp.volumn >= 200)
                milk[i++] = temp;
            else
                n--;
        }
        qsort(milk, n, sizeof(milk[i]), cmp);
        cout << milk[n-1].name << endl;
    }
    return 0;
}


HDOJ 1070 Milk