Niuniu loves to drink

Niuniu loves to drink

Title description

Niuniu is an alcoholic and loves to drink very much. A bottle of wine costs RMB m, two bottles can be exchanged for a bottle of wine, four bottle caps can be exchanged for a bottle of wine, now there is n RMB, how many bottles of wine can you drink?

(Note: There is no loan function, that is, it is not allowed to borrow a bottle of wine and exchange the bottle for return after drinking)

Topic analysis

This is obviously an elementary school Mathematical Olympiad question. Use bottles and bottle caps for wine. The idea is to update the value of the bottle cap and the value of the bottle and the amount of wine drunk from time to time, and then cycle to the end (that is, there are not enough caps four, not enough empty bottles Two)

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n ,m;
    cin>>m>>n;
    int ans = n / m;//第一步就更新喝的酒的个数
    int g = ans;//盖子数
    int p = ans;//瓶子数
    while(1)//直接循环,找机会break就可
    {
        if(p < 2 && g < 4) break;//跳出循环
        else
        {
            if(p >= 2)//看看瓶子能不能换酒
            {
                ans += p / 2;//更新的是喝的酒的数
                g += p / 2;//记得把盖子的数也更新一下
                p = p / 2 + p % 2;//因为你花了空瓶子,但又换了空瓶子,就得记录好空瓶子的数量,一部分是换空瓶子剩下的,另一部分是换了新的酒得到的空瓶子
            }
            if(g >= 4)//同上
            {
                ans += g / 4;
                p += g / 4;
                g = g / 4 + g % 4;
            }
        }
    }
    cout<<ans;//输出ans就可以了
}

Guess you like

Origin blog.csdn.net/weixin_51216553/article/details/110882095