动态规划--钢条切割问题
2019-12-30 本文已影响0人
暮想sun
给定一个长度为n英寸的钢条和一个价格表pi,求切割钢条方案,使得销售收益rn最大。
public static int steelBarCut(int[] price, int n) {
//构造收益数组,存储对应钢条长度收益最大值
int[] profit = new int[n + 1];
profit[0] = 0;
for (int i = 1; i <= n; i++) {
int max = Integer.MIN_VALUE;
for (int k = 1; k <= i; k++) {
//比较当前最大值与,最新切割方案的数据最大值
max = Math.max(max, price[k] + profit[i-k]);
}
profit[i] = max;
}
return profit[n];
}