剑指Offer-12剪绳子

46 阅读1分钟
public static int cuttingRope(int n) {
    int max = 0;
    for (int i = 2; i<= n; i++){
        max = Math.max(cutting(n, i), max);
    }
    return max;
}

public static int cutting(int n, int m){
    // 每段的长度 只能是整数
    int length = n / m;
    // 剩余的长度
    int reduce = n % m;
    // 因为存在剩余长度且不大于m 根据平均分乘积最大的原理 有reduce根绳子+1做到最平均 得到下面的式子
    int mul = (int) (Math.pow(length, m - reduce) * Math.pow(length + 1, reduce));
    return mul;
}