喝汽水,1瓶汽水1元,2个空瓶可以换一瓶汽水, 给20元,可以多少汽水

代码直接奉上

#include<stdio.h>
int main()
{
    int cash = 20;
	int total =cash ;	
	int empty = cash;	
	while(empty > 1)
	{
		total += empty / 2;//你现在的空瓶子可以换几瓶
		empty = empty / 2 + empty % 2;//你有多少空瓶子
	}	
	printf("%d",total);
	return 0;
}

笔者在写这个简单的小程序时想,那我总是留下最后一个空瓶,那我和老板借一瓶喝完我就有了俩个空瓶子,刚好等于一瓶饮料还回去不就好了(哈哈哈,只是玩笑)。其实我们在改变钱的数量时会发现我们可以喝的水总等于2cash - 1,就直接可以输出2cash - 1,这样这个程序就变得更简单了。

猜你喜欢

转载自blog.csdn.net/lucky52529/article/details/84063969