#include
#include
#include
using namespace std;
struct milk
{
string ch;
double jg; //jg 用double
int sl;
}leixing1[101];
int main()
{
int ri,repeat,i,j,d,k,n; // n 用int
double s,min; // s,min 用double
string ch1;
cin>>repeat;
for(ri=0;ri
min=100000000.0;
cin>>n;
for(i=0;i
cin>>leixing1[i].ch>>leixing1[i].jg>>leixing1[i].sl;
}
for(i=0;i
if(leixing1[i].sl<200)
continue;
else if(leixing1[i].sl>1000)
{
d=5;
s=leixing1[i].jg/d;
if(s
min=s;
ch1=leixing1[i].ch;
k=i;
}
}
else
{
d=leixing1[i].sl/200;
s=leixing1[i].jg/d;
if(s
min=s;
ch1=leixing1[i].ch;
k=i;
}
}
}
cout<
return 0;
}
在第一种情况下,伊利牛奶可以喝了2天,费用10元。蒙牛牛奶可以喝5天,它的成本20元。因此,蒙牛是最便宜的。
在第二种情况下,光明牛奶应该被忽略。烟牌牛奶可以喝5天,但它的成本40元。因此,蒙牛是最便宜的。
如果有超过一个最便宜的品牌,你应该选择容量最大的那个品牌。
AC代码,要小心只能喝五天
#include
using namespace std;
struct Node
{
char name[102];
int price;
int ml;
int per;
}Milk[102];
int pri(Node&mi)
{
Node mil = mi;
while (mil.ml > 200)
{
mil.ml -= 200;
mil.per++;
if (mil.per == 5) //最多只能喝5天
{
break;
}
}
return mil.per;
}
int main()
{
int t;
cin>>t;
while (t--)
{
Milk[101].price = 100000;
Milk[101].per = 1;
int n;
cin>>n;
for (int i=0; i
cin>>Milk[i].name>>Milk[i].price>>Milk[i].ml;
Milk[i].per = 0;
}
for (int i=0;i
if(Milk[i].ml < 200)
continue;
else
{
Milk[i].per = pri(Milk[i]);
if(Milk[i].price/Milk[i].per < Milk[101].price/Milk[101].per) //计算性价比
{
Milk[101].price = Milk[i].price;
Milk[101].per = Milk[i].per;
strcpy(Milk[101].name,Milk[i].name);
Milk[101].ml = Milk[i].ml;
continue;
}
if(Milk[i].price/Milk[i].per == Milk[101].price/Milk[101].per) //性价比相同选容量大的
{
if (Milk[i].ml > Milk[101].ml)
{
Milk[101].price = Milk[i].price;
Milk[101].per = Milk[i].per;
strcpy(Milk[101].name,Milk[i].name);
Milk[101].ml = Milk[i].ml;
}
}
}
}
cout<
return 0;
}
我提交了下,应该是float jg和float s,n,min的问题,替换为int之后,顺利AC。