![](https://i-blog.csdnimg.cn/blog_migrate/e0dfcce678651a93bef0aa75e482e417.png)
Java程序:
public class MultiPack {//多重背包
public static void main(String[] args) {
int[] weight = new int[] {1, 3, 4};
int[] value = new int[] {15, 20, 30};
int[] nums = new int[] {2, 3, 2};
int bagWeight = 10;
testMultiPack(weight,value,nums,bagWeight);
}
public static void testMultiPack(int[] weight,int[] value,int[] nums,int bagWeight) {
int[] dp=new int[bagWeight+1];
for (int i = 0; i < weight.length; i++) {
for (int j = bagWeight; j >= weight[i]; j--) {
for (int k = 1; k <= nums[i]&&(j-k*weight[i]>=0); k++) {
dp[j]=Math.max(dp[j],dp[j-k*weight[i]]+k*value[i]);
}
}
}
for(int i:dp){
System.out.println(i+" ");
}
}
}