可乐商店问题
题目大意:
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
题目分析:
这是一道数学问题,考验的是数学思考能力。根据题中给出的示例可以让我们更清楚的计算出结果。用到了取余数与取除数(n=n/3+n%3),每次都要统计所喝的瓶子的总数量。
如果还剩两个空瓶子时可以借一个空瓶子再喝一瓶可乐。当n=0时不执行,程序结束。
程序代码:
1 #include<cstdio> 2 #include<iostream> 3 using namespace std; 4 5 int n[100]; 6 7 int main() 8 { 9 int n,a; 10 while(scanf("%d",&n)) //输入空瓶子数量 11 { 12 if(n==0) //当n=0时程序结束 13 break; 14 a=0; 15 while(n>2) //n>2时做此循环,统计所喝饮料的总数a 16 { 17 a+=n/3; 18 n=n/3+n%3; 19 } 20 if(n==2) 21 a++; 22 cout<<a<<endl; 23 } 24 return 0; 25 }
心得:
这道题主要考数学思考能力,把要做的公式想到,题目就变得简单了,而且题中还给了提示。虽然一道很简单的题目,我却做了一个多小时,有一处错误总也改不对,后来静下心来终于改对了,是一处很小的错误,这提醒我以后写程序时要注意细节,不要大体过了就算了。