喝汽水,1瓶汽水1元,2个空瓶可以换1瓶汽水,给20元可以喝多少汽水?
#include<stdio.h>
int main()
{
int money = 0;
int empty = 0;
int total = 0;
scanf("%d", &money);
//买回来的汽水喝掉
total = money;
empty = money;
while (empty>=2)
{
total += empty / 2;//用空瓶子换的汽水
empty = empty / 2 + empty % 2;//换的汽水喝完后的瓶子+之前剩的瓶子
}
printf("%d", total);
return 0;
}
用等价的方法:
1瓶汽水1元,2个空瓶可以换1瓶汽水==1个空瓶0.5元
那么20元可以买40个空瓶,无论如何最后都只剩一个空瓶换不了,所以喝了39瓶
即2n-1
其实也可以和40瓶,你先向老板借一个空瓶子,再和剩下的一个瓶子换一瓶汽水,喝完将空瓶子换给老板(哈哈哈)