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.
SpoilerLet 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
代码实现
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int n;
while(scanf("%d",&n)==1&&n)
{
if(n%2==0)
printf("%d\n",n/2);
if(n%2==1)
printf("%d\n",(n-1)/2);
}
return 0;
}
题解
列出n从 0到10的结果
1……0;
2……1
3……1
4……2
5……2
6……3
7……3
8……4
9……4
10……5
发现规律:
当n为奇数时
得到的可乐为(n-1)/2瓶
当n为偶数时
得到的可乐为 n/2 瓶