1. 问题描述:
2. 核心思想
问题结构分析——递推关系建立——自底向上计算——最优方案追踪
3. 算法伪代码
核心算法
3. Java代码实现
// rodCutting Algorithm
public class rodCutting {
public static void main(String[] args){
int[] input = {1,5,8,9,10,17,17,20,24,24};
int result = rodCutting(input, input.length);
System.out.println("最大收益"+result);
}
private static int rodCutting(int[] p, int n){
int[] C = new int[n+1];
int[] rec = new int[n+1];
C[0] = 0;
int q;
for(int j=1; j<=n; j++){
q = p[j-1];
rec[j] = j;
for(int i=1; i<=j-1; i++){
if(q<(p[i-1]+C[j-i])){
q = p[i-1]+C[j-i];
rec[j] = i;
}
}
C[j] = q;
}
while(n>0){
System.out.println(rec[n]);
n = n-rec[n];
}
return C[p.length];
}
}