public class Test54 { //给定两个长度都为N的数组weights和values //weights[i]和values[i]分别表示i号物品的重量和价值 //给定一个正数bag表示一个载重bag的袋子 //装的物品不能超过这个重量 //返回最多装的价值 public static int dpWay(int[] w, int[] v, int bag) { int N = w.length; int[][] dp = new int[N + 1][bag + 1]; for (int index = N - 1; index >= 0; index--) { for (int rest = 1; rest <= bag; rest++) { int p1 = dp[index + 1][rest]; int p2 = -1; if (rest - w[index] >= 0) { p2 = v[index] + dp[index + 1][rest - w[index]]; } dp[index][rest] = Math.max(p1,p2); } } return dp[0][bag]; } }
02-13
4万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-22
3万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-16
1180
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)