HDU—— 1070 Milk

题意:Ignatius喝牛奶,并且有几个原则:1、不喝6天前的牛奶,即一盒牛奶最多连续喝5天,第6天就不再喝了。2、每天和200ml。3、当剩下牛奶小于200ml时就扔掉。4、超市所有牛奶都是同一天生产。现在求买哪种牛奶最便宜。输入格式为:第一行一个T表示测试用例个数,接着每个测试第一行输入一个N表示牛奶的种类,然后N行每行分别输入brand(字符串)、price、volume。

解题思路:详见代码。

#include <iostream>
#include <string>
using namespace std;

struct Node//定义一个结构体
{
    string brand;//牛奶品牌
    int Price;//牛奶价格
    int volume;//牛奶体积
}Brands[105];//结构体数组

int main()
{
    //freopen("input.txt","r",stdin);
    int T,N,k;
    cin>>T;
    while(T--)
    {
        cin>>N;
        double cheap = 100000000.0;//定义一个中间量来接收最便宜价格
        string cBrand; int volumes = 200;//brand接收最便宜牛奶名称,volume接收最便宜牛奶体积
        for(int i = 1; i<=N; i++)
        {
            cin>>Brands[i].brand>>Brands[i].Price>>Brands[i].volume;
            if(Brands[i].volume < 200) continue;//当体积小于200时忽略
            else if(Brands[i].volume > 1000)//当体积大于1000时直接按1000计算
            {//每次判断是特别注意当出现多个最便宜价格一样时,选择体积最大的
                if(cheap > Brands[i].Price*1.0/5 ||(cheap == Brands[i].Price*1.0/5 &&Brands[i].volume > volumes))
                { cheap = Brands[i].Price*1.0/5; cBrand =Brands[i].brand; volumes = Brands[i].volume; }
            }
            else
            {//每次判断是特别注意当出现多个最便宜价格一样时,选择体积最大的
                int n = Brands[i].volume/200;
                if(cheap > Brands[i].Price*1.0/n ||(cheap == Brands[i].Price*1.0/n&&Brands[i].volume > volumes))
                { cheap = Brands[i].Price*1.0/n; cBrand =Brands[i].brand; volumes = Brands[i].volume;}
            }
        }
        cout<<cBrand<<endl;//最后输出最便宜牛奶的品牌
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值