2015 HUAS Provincial Select Contest #3~C

11877 The Coco-Cola

Store Once upon a time, there is a special coco-cola store. If you return three empty bottles to the shop, you’ll get a full bottle of coco-cola to drink. If you have n empty bottles right in your hand, how many full bottles of coco-cola can you drink?

Input

There will be at most 10 test cases, each containing a single line with an integer n (1 ≤ n ≤ 100). The input terminates with n = 0, which should not be processed.

Output

For each test case, print the number of full bottles of coco-cola that you can drink.

Spoiler

Let me tell you how to drink 5 full bottles with 10 empty bottles: get 3 full bottles with 9 empty bottles, drink them to get 3 empty bottles, and again get a full bottle from them. Now you have 2 empty bottles. Borrow another empty bottle from the shop, then get another full bottle. Drink it, and finally return this empty bottle to the shop!

Sample Input

3

10

81

0

Sample Output

1

5

40

解题思路:首先这个程序是以0为结束标志,当输入的数字为零时程序便结束。题目的意思是在商店用3个空瓶子换取一瓶牛奶,当你的手上有两个空瓶子时你可以向商店老板借一瓶牛奶,喝完以后你将手里三个空瓶子都给老板即可,但是当你手上只有一个空瓶子时,你就不可以向老板借牛奶了,此时程序就应该终止了,就应该输入下一个数字。这里应该注意的事是,你在输入下一个数字之后应该对计算的应得牛奶总数进行清零,不然会产生错误,每循环一次还要判断一下手里的空瓶子还有多少,如果少于3还要进行进一步判断,看是否还要向老板借牛奶,这样程序才不会出错。

程序代码:

#include<cstdio>
int main()
{
	int n;
	while(scanf("%d",&n)==1&&n)
	{   
		int sum=0;
		while(n>=3)
		{
		sum=sum+n/3;
		n=n/3+n%3;
		}
		if(n==0||n==1)
			printf("%d
",sum);
		else  printf("%d
",sum+1);
	}
	return 0;
}
原文地址:https://www.cnblogs.com/chenchunhui/p/4655441.html