HDU1070 - Milk

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1070

解题思路:主要考察C结构体排序。

#include <bits/stdc++.h>
using namespace std;
//考察结构体排序 
struct milk {
    string s;
    int cost;
    int volume;
};
bool cmp(milk a, milk b) {
    if (a.cost < b.cost) {
        return true;
    } else if (a.cost == b.cost) {
        return a.volume > b.volume; 
        //如果一样便宜,选容量多的那个 
    } else {
        return false;
        //还要考虑a.cost > b.cost的情况 
    }
}
int main() {
    milk a[105];
    int t, n, p, v, day;
    string s; 
    cin >> t;
    while (t--) {
        cin >> n;
        int cnt = 0;
        while (n--) {
            cin >> s >> p >> v;
            if (v < 200) continue;
            //忽略小于200ml的牛奶 
            a[cnt].s = s;
            a[cnt].volume = v; 
            day = a[cnt].volume / 200;
            if (day > 5) day = 5; 
            //不喝6天前的奶,如果大于5天,就取5天 
            a[cnt].cost = p / day;    
            cnt++;
        }
        sort(a, a+cnt, cmp);
        cout << a[0].s << endl;
    }
    return 0;
} 
posted @ 2019-12-21 21:10  oeong  阅读(239)  评论(0编辑  收藏  举报