剑指 Offer 14- I. 剪绳子
package leetcode; public class offer_14_1 { public int cuttingRope(int n) { int max=1; int[] dp=new int[n+1]; //当n≤3时,不符合动态规划 if(n<=3) { return Math.max(n-1, 1); } //当n≥4时,动态规划将n分成两部分,获取乘机的最大值 dp[1]=1; dp[2]=2; dp[3]=3; for(int i=4;i<=n;i++) { for(int j=1;j<=i/2;j++) { max=Math.max(max, dp[j]*dp[i-j]); } dp[i]=max; } max=dp[n]; return max; } public static void main(String[] args) { // TODO Auto-generated method stub offer_14_1 off=new offer_14_1(); System.out.println(off.cuttingRope(11)); } }