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 manyfull 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). Theinput 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 emptybottles, drink them to get 3 empty bottles, and again get a full bottle from them. Now you have 2empty bottles. Borrow another empty bottle from the shop, then get another full bottle. Drink it, andfinally return this empty bottle to the shop!
Sample Input
3
10
81
0
Sample Output
1
5
40
题目大意:在一家可乐店里可以用三个空可乐瓶兑换1瓶可乐,现在给你一堆空可乐瓶,求你能喝多少瓶可乐。
解题思路:假设有x个可乐瓶,那么可以兑换x / 3瓶可乐,共剩余x / 3 + x % 3个瓶子,递归处理,注意递归边界。
代码如下:
#include <bits/stdc++.h>
using namespace std;
int fun(int x)
{
int sum = 0;
if(x == 2) return 1;
else if (x == 1 || x == 0) return 0;
else{
int t1,t2;
t1 = x / 3;
t2 = x % 3;
t2 += t1;
sum = t1 + fun(t2);
}
return sum;
}
int main(void)
{
int n;
while(cin >> n && n){
cout << fun(n) << endl;
}
return 0;
}