public class E14CuttingRope {
public static int maxResultsAfterCutting_Solution1(int length) {
if (length < 2)
return -1;
if (length == 3)
return 2;
int[] results = new int[length + 1];
results[0] = 0;
results[1] = 1;
results[2] = 2;
results[3] = 3;
int max = 0;
for (int i = 4; i <= length; i++) {
for (int j = 1; j <= i / 2; j++) {
results[i] = results[j] * results[i - j];
if (max < results[i])
max = results[i];
}
results[i] = max;
}
return results[length];
}
public static int maxResultsAfterCutting_Solution2(int length){
int maxThreeTimes = length / 3;
if (length % 3 == 1)
maxThreeTimes -= 1;
int maxTwoTimes = (length - maxThreeTimes * 3) / 2;
return (int)Math.pow(3, maxThreeTimes) * (int)Math.pow(2, maxTwoTimes);
}
public static void main(String[] args){
System.out.println(E14CuttingRope.maxResultsAfterCutting_Solution1(4));
System.out.println(E14CuttingRope.maxResultsAfterCutting_Solution1(5));
System.out.println(E14CuttingRope.maxResultsAfterCutting_Solution2(4));
System.out.println(E14CuttingRope.maxResultsAfterCutting_Solution2(5));
}
}