关于剪绳子的题目,第一步要想到为拿到8米长绳子的最大乘积,记为f(8),使用贪心算法,我们认为f(8)的最大值在f(n)*f(8-n)中诞生,所以去遍历数组得到max值。
public class Solution { public int cutRope(int target) { if(target==2){return 1;} if(target==3){return 2;} int [] rope = new int[target+1]; rope[0]=0; rope[1]=1; rope[2]=2; rope[3]=3; int temp = 0; int max = 0; for(int i=4;i<=target;i++){ for(int j=0;j<=i/2;j++){ temp = rope[j]*rope[i-j]; if(max<temp){max=temp;} } rope[i]=max; } return rope[target]; } }