public class Pack {
int totalWeight = 100;
int weight[] = { 10, 10, 20, 30, 40, 40, 20 };
int maxWeight[][] = new int[totalWeight + 1][weight.length + 1];
public static void main(String[] args) {
Pack pack = new Pack();
int result = pack.getMaxWeight(pack.totalWeight, pack.weight.length - 1);
System.out.println(result);
}
int getMaxWeight(int totalWeight, int num) {
int maxResult = 0;
if (this.maxWeight[totalWeight][num] != -1) {
maxResult = maxWeight[totalWeight][num];
}
if (totalWeight >= weight[num]) {
if (num == 0) {
maxResult = weight[num];
} else {
int result1 = getMaxWeight(totalWeight - weight[num], num - 1) + weight[num];
int result2 = getMaxWeight(totalWeight, num - 1);
maxResult = max(result1, result2);
}
}
return maxResult;
}
int max(int a, int b) {
return a > b ? a : b;
}
}
简单装箱
最新推荐文章于 2021-07-27 11:02:51 发布