2个汽水空瓶或者4个瓶盖可以换1瓶汽水,现在有n瓶汽水,问可以多喝到多少瓶汽水。希望各位大佬给个c语言的模板
2个汽水空瓶或者4个瓶盖可以换1瓶汽水,现在有n瓶汽水,问可以多喝到多少瓶汽水
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; }
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-6 12:33编辑过]
[此贴子已经被作者于2017-12-6 19:59编辑过]