14 剪绳子

package sort;

public class Test14 {

    public static void main(String[] args) {
        System.out.println(cut(8));
    }

    public static int cut(int a) {
        int[] store = new int[a + 1];
        if (a == 0)
            return 0;
        if (a == 1)
            return 0; // 不符合规律,直接手动定义
        if (a == 2)
            return 1;

        if (a == 3)
            return 2;
        for (int i = 0; i <= a; i++) {
            if (i == 0)
                store[i] = 0; // 利用数值存储已经计算过的,方便后来者进行计算
            if (i == 1)
                store[i] = 1;
            if (i == 2)
                store[i] = 2;
            if (i == 3)
                store[i] = 3;
            if (i >= 4) {        //如果大于大于4,则开始利用前面数组进行计算
                              //例如4可以分成1+3,2+2,求出最大值放入数组4位置
                int max = 0;
                for (int j = 1; j <= i / 2; j++) {
                    if (store[j] * store[i - j] > max)
                        max = store[j] * store[i - j];

                }
                store[i] = max;

            }

        }
        return store[a];

    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值