Competition--Coke shop--Problem Solving report

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.