Blue Bridge Cup 2018 Preliminary-Monkeys divide bananas

Title description

The 5 monkeys are good friends and fell asleep on the coconut tree by the sea. During this period, some merchant ships forgot a bunch of bananas and left on the beach.
The first monkey woke up and divided the bananas into 5 piles. Only one was left, and he ate it and hid his share and continued to sleep.
The second monkey woke up, divided the bananas into 5 piles again, and there were 2 remaining, so he ate it and hid his share and continued to sleep.
The third monkey woke up, divided the bananas into 5 piles again, and there were 3 left, so he ate it and hid his share and continued to sleep.
The fourth monkey woke up, divided the bananas into 5 piles again, and there were 4 remaining, so he ate it and hid his share and continued to sleep.
The fifth monkey wakes up and divides the bananas into 5 piles again, haha, just not left!
Please calculate how many bananas there are at least at the beginning.

Output

Output an integer to indicate the answer

code show as below:

#include <iostream>
using namespace std;

int main()
{
    
    
        for(int i=1;;i++){
    
    
            int sum = i; //用i表示香蕉的数量,因为后续会对i进行多次重新赋值,因此定义一个sum来接收i
            if(sum%5==1){
    
    
                sum = sum-1-sum/5;//猴子会吃掉多余的并藏起自己那份
                if(sum%5==2){
    
    
                    sum = sum-2-sum/5;
                    if(sum%5==3){
    
    
                        sum = sum-3-sum/5;
                        if(sum%5==4){
    
    
                            sum = sum-4-sum/5;
                            if(sum%5==0&&sum!=0){
    
    //最后香蕉数量不能为0,这一步很关键,没有这一步会得出错误答案
                                cout<<i<<endl;//最后输出的应当是i的值
                                return 0;
                            }
}
}
}}}}

Guess you like

Origin blog.csdn.net/m0_51955470/article/details/113666946