Quick actions

cmd+k|ctrl+k

Navigation

Languages

N个小熊分苹果

Snippet info

Language

Java

Visibility

public

Author

subchen

Created

2015-09-24T02:01:17Z

Updated

2015-09-24T02:05:16Z

// N(1<=N<=9)个小熊分一堆苹果,
// 第一只小熊将苹果分成N份,多了一个,扔掉,然后拿走自己的那一份。
// 第二只熊将剩余的苹果分成N份,又多了一个,扔掉,然后拿走自己的那一份,
// 第三只.....,直到第N只熊;问最初的苹果有多少个?

class Main {

    public static int n = 9;//熊数N
    public static int a = (int)Math.pow(n, n-1);//N^(N-1)
    public static int b = (int)Math.pow(n-1, n-1);//(N-1)^(N-1)
     
    public static void main(String[] args){
         
        for(int i=1; i<=Integer.MAX_VALUE; i++){
            if(solve(i*n+1)){
                System.out.println(n+" "+(i*n+1));
                break;
            }
        }
    }
    //假设第n只熊拿到An个苹果,那么N*An=(N-1)*A(n-1) - 1;
    //得到递推式:N*(An+1) = (N-1)*(A(n-1) + 1):
    public static boolean solve(int apple){
        //第一只熊拿到的苹果数
        int a0 = (apple-1)/n+1;
        int b0 = b*a0;
         
        boolean flag =(b0/a*a == b0);
         
        if(flag){
            return true;
        }
        return false;
    }

}
INFO