Fruit[] fruits={
new Fruit(“李子”, 4, 4500),
new Fruit(“苹果”, 5, 5700),
new Fruit(“桔子”, 2, 2250),
new Fruit(“草莓”, 1, 1100),
new Fruit(“甜瓜”, 6, 6700)
};
int[] items = new int[MAX+1];
int[] value = new int[MAX+1];
for(int i=0;i<fruits.length;i++){
for(int s = fruits[i].getSize();s<=MAX;s++){
int p = s-fruits[i].getSize();
int newValue = value[p] + fruits[i].getValue();
if(newValue>value[s]){
value[s] = newValue;
items[s] = i;
/* */
}
}
}
System.out.println(“物品/t价格”);
for(int i = MAX;
i >= MIN;
i = i – fruits[items[i]].getSize()
){
System.out.println(fruits[items[i]].getName()+”/t”+fruits[items[i]].getValue());
/* 注意是fruits[item[i]]*/
}
System.out.println(“合计:”);
System.out.println(value[MAX]);
}