题意: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;
}