每天一题(7)-分苹果

每天一题-分苹果

题目

在这里插入图片描述

代码

import java.util.Scanner;

/**
 * 题目描述:
 * https://exercise.acmcoder.com/online/online_judge_ques?ques_id=1654&konwledgeId=134
 *
 * 果园里有堆苹果,N(1<N<9)只熊来分。第一只熊把这堆苹果平均分为N份,多了一个,它把多的一个扔了,拿走了一份。
 * 第二只熊把剩下的苹果又平均分成N份,又多了一个,它同样把多的一个扔了,拿走了一份,第三、第四直到第N只熊都是这么做的,
 * 问果园里原来最少有多少个苹果?
 *
 * 输入:输入1个整数,表示熊的个数。它的值大于1并且小于9。
 * 输出:为1个数字,表示果园里原来有的苹果个数。
 * 样例输入:5
 * 样例输出:3121
 *
 * @author ydfind
 * @date 2019.08.06
 */
public class Main {

    private static int calc(int n){
        int d = 0;
        int next = -1;
        while (++d < 1000000 && next <= 0){
            // 最后一次分剩下的数量必然是n的倍数加1
            next = d * n + 1;
            for(int i = 0; i < n - 1; i++){
                if(next % (n - 1) != 0) {
                    next = -1;
                    continue;
                }
                next = next / (n - 1) * n + 1;
            }
        }
        return next;
    }

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        while(cin.hasNextInt()) {
            int n = cin.nextInt();
            int count = calc(n);
            System.out.println(count);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值