Lanqiao Cup - [Sixth] Beverage Exchange [Group B]

Leyangyang Beverage Factory is holding a promotional event. LeYangYang C-type drink can be exchanged for another C-type drink with 3 bottle caps, and the cycle can continue (but temporary loans or credit are not allowed).

Please calculate, if Xiao Ming does not waste bottle caps and participates in activities as much as possible, then for the n bottles of drinks he initially bought, how many bottles of drinks he can drink in the end.

Input format

Enter an integer n n, representing the initial number of drinks purchased.

Output format

Output an integer representing the total number of drinks that can be consumed.

data range

0<n<10000

Input example 1:

100

Output sample 1:

149

Input example 2:

101

Output sample 2:

151

______________________________________________________________________________________________________

Resource agreement:

  Peak memory consumption (including virtual machines) < 256M

  CPU consumption < 1000ms

  Please output strictly according to the requirements and do not print unnecessary content similar to: "Please enter...".

  All codes are placed in the same source file. After debugging is passed, copy and submit the source code.

  Note: Do not use package statements. Do not use features of jdk1.7 and above.

  Note: The name of the main class must be: Main, otherwise it will be treated as invalid code.

______________________________________________________________________________________________________

Original title link

Idea: Simply simulate it and you will find the pattern.
Text explanation:
Assume that there are 10 bottles of drinks at the beginning, and each bottle of drink can get a bottle cap. So there are 10 bottles of drinks at the beginning. ​Drink these 10 bottles of drinks
first
n = 10 ​Exchange
these
(n/3+n%3) ​Exchange
these
Then drink this bottle of drink and get a bottle cap. At this time, you have 2 bottle caps in your hand. (Cannot be replaced!)

Formula extraction:
Take 10 bottles as an example, let n be the bottle purchased at the beginning, m be the bottle that can be directly exchanged, and x be the remaining bottle after exchange
​①
n = 10, m = 10/3 = 3 x = 1
​②
n = 3 + 1 = 4,m = 4/3 = 1 x = 1
​③Because
the number of bottle caps left on hand is 1 + 1 < 3, therefore, the total number that can be exchanged is **m total = 3 + 1 = 4**

It becomes easy to see the pattern

The formula that becomes n, m, into new n)

Drawing:

Code part:

#include<iostream>
#include<algorithm> 
using namespace std;
int main()
{
    int n,m = 0,x = 0;  //定义变量n为初始饮料数量,m为最多可以喝到的饮料数量,x暂未使用
    cin >> n;           //输入初始饮料数量
    m = n;              //将初始饮料数量加入最多可以喝到的饮料数量中,因为最初买入的饮料可以直接喝
    while(n >= 3)       //只有当剩余的饮料数量大于等于3时才进行兑换
    {
        m += n/3;     //计算可以直接兑换得到的饮料数量,加入最多可以喝到的饮料数量中
        n = n/3 + n%3; //计算喝掉的饮料数量,加上剩余的瓶盖数量和空瓶子数量,作为下一轮的初始饮料数量
    }
    cout<<m;            //输出最多可以喝到的饮料数量    
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_29992017/article/details/129650987