2015 HUAS Provincial Select Contest #3 C

题目:

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!

题目大意:有N个空杯子,3个杯子能换一杯满的饮料,求最多能喝多少杯?

解题思路:sum+=(n/3); n=n%3+n/3用这两个式子,第一个存储商,一个存储余数加商做下轮的除数,当n=2是可以先喝一杯再把3个杯子叫上去。n<=1是可以直接退出循环。

代码: 

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 int main()
 5 {
 6     int n,sum;
 7     while(cin>>n)
 8     {
 9         if(n>=1&&n<=100)
10         {
11             sum=0;
12             while(1)
13             {
14                 if(n==2)
15                 {
16                     sum+=1;
17                     break;
18                 }
19                 else 
20                     sum+=(n/3);
21                 n=n%3+n/3;
22                 if(n<=1)
23                     break;
24             }
25             printf("%d\n",sum);
26         }
27         else break;
28     }
29     return 0;
30 }
31  
32 
33  

 


				else 
					sum+=(n/3);
				n=n%3+n/3;
				if(n<=1)
					break;
			}
			printf("%d\n",sum);
		}
		else break;
	}
	return 0;
}

 

 

posted on 2015-07-16 20:16  最爱剪刀手  阅读(110)  评论(0编辑  收藏  举报