2个汽水空瓶或者4个瓶盖可以换1瓶汽水,现在有n瓶汽水,问可以多喝到多少瓶汽水。希望各位大佬给个c语言的模板
2个汽水空瓶或者4个瓶盖可以换1瓶汽水,现在有n瓶汽水,问可以多喝到多少瓶汽水
2017-12-05 21:29
2017-12-05 22:14
2017-12-05 22:16
程序代码: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-05 22:17
程序代码:unsigned foo( unsigned n )
{
unsigned a=n, b=n, c=n;
for( ; a>=2 || b>=4; )
{
unsigned t = a/2 + b/4;
a = a%2 + t;
b = b%4 + t;
c += t;
}
return c;
}unsigned bar( unsigned n )
{
return n<2 ? n : 4*n-5;
}
2017-12-06 09:08

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

2017-12-06 11:38

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

2017-12-06 19:53
2017-12-07 08:48
2017-12-07 09:53