简单装箱

来源:互联网 发布:北京幼儿园学费知乎 编辑:程序博客网 时间:2024/05/16 01:18
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;    }}
0 0
原创粉丝点击