Coke Shop problem
Main topic:
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 had n empty bottles right in your hand, how many
Full bottles of Coco-cola can you drink?
Requirements:
Input
There'll is at the most of the test cases, each containing a, and an integer n (1≤n≤100). The
Input terminates with n = 0, which should isn't be processed.
Output
For each test case, print the number of full bottles of coco-cola so you can drink.
Spoiler
Let me-tell-you-drink 5 full bottles with ten 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 we have 2
Empty bottles. Borrow another empty bottle from the shop and then get another full bottle. Drink it, and
Finally return this empty bottle to the shop
Sample input:
Sample Input
3
10
81
0
Sample Output
1
5
40
Topic Analysis:
This is a mathematical problem, the test is the ability of mathematical thinking. According to the example given in the question, we can calculate the result more clearly. Use the remainder and the divisor (n=n/3+n%3), each time to count the total number of bottles to drink.
If you have two empty bottles left, you can borrow an empty bottle and drink a bottle of Coke. When N=0 is not executed, the program ends.
Program code:
1#include <cstdio>2#include <iostream>3 using namespacestd;4 5 intn[ -];6 7 intMain ()8 {9 intN,a;Ten while(SCANF ("%d", &n))//Enter the number of empty bottles One { A if(n==0)//program ends when N=0 - Break; -A=0; the while(n>2)//N>2 When doing this cycle, statistics the total number of drinks drink a - { -a+=n/3; -n=n/3+n%3; + } - if(n==2) +a++; Acout<<a<<Endl; at } - return 0; -}
Experience:
This problem is the main test of mathematical thinking ability, the formula to do think, the topic becomes simple, and the question back to the hint. Although a very simple topic, I did one hours, there is a mistake always change the wrong, and then the quiet heart to finally changed to, is a very small mistake, this reminds me to pay attention to the details after the program, do not generally over the forget.
Competition--Coke shop--Problem Solving report