2个汽水空瓶或者4个瓶盖可以换1瓶汽水,现在有n瓶汽水,问可以多喝到多少瓶汽水。希望各位大佬给个c语言的模板
2个汽水空瓶或者4个瓶盖可以换1瓶汽水,现在有n瓶汽水,问可以多喝到多少瓶汽水
a个盖,b个瓶
可喝得 a/4 + b/2 瓶汽水
变为 a/4 + b/2 + a%4 个盖,a/4 + b/2 + b%2 个瓶
循环
程序代码:int DrinkGas(int n)
{
int gas=n;
int bottle=n;
int lid=n;
int tp;
while(bottle>=2 || lid>=4)
{
while(bottle>=2)
{
tp=bottle/2;
bottle=bottle%2;
gas+=tp;
bottle+=tp;
lid+=tp;
}
while(lid>=4)
{
tp=lid/4;
lid=lid%4;
gas+=tp;
lid+=tp;
bottle+=tp;
}
}
printf("bottle=%d,lid=%d\n",bottle,lid);
return gas;
}


~[此贴子已经被作者于2017-12-6 12:33编辑过]

~[此贴子已经被作者于2017-12-6 19:59编辑过]
