The monkey divides the peach problem

topic

Question: There are a bunch of peaches on the beach, and five monkeys will divide them. The first monkey divided the pile of peach vouchers into five parts, one more, the monkey threw the more one into the sea and took one. The second monkey divided the remaining peaches into five equal parts, and added one more. It also threw the extra peaches into the sea and took one part. The third, fourth, and fifth monkeys did the same. Yes, how many peaches were there at least on the beach?

If there are N (1 < N) monkeys on the beach, according to the above method, how many peaches are there at least on the beach? If there is only one monkey on the beach, according to the above rules, it will be thrown into one, then divided into 1, and then taken away, which will look more strange. Unreasonable and reasonable, so limit N>1.

code

/* 算法的效率并不高,从桃子总数total为1开始测试。保证后续分配是整数就是合理的。
 */
public class MonkeyDemo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int number = scanner.nextInt(); //输入猴子的数目
        int i, total = 1, x = 1;
        while (true) {
            x = total;
            for(i = 0; i < number; i++){
                if((x-1) > 0 && (x - 1) % number == 0){ //关键在于保证是整除的关系
                    x = (x - 1)*(number-1)/(number);
                }
                else{
                    break;
                }
            }
            if(i == number)
                break;
            total++;
        }
        System.out.println(total);
        scanner.close();
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325714499&siteId=291194637