public static void main(String[] args) { int[] value = {100, 150, 350}; int[] weight = {1, 4, 3}; System.out.println(dynamic(5, value, weight)); } /** * 动态规划-背包问题 * * @param W * @param value * @param weight * @return */ public static int dynamic(int W, int[] value, int[] weight) { int N = value.length; int[][] op = new int[N + 1][W + 1]; if (value.length != weight.length) { return 0; } for (int i = 1; i <= N; i++) { for (int j = 0; j <= W; j++) { if (j >= weight[i - 1]) { op[i][j] = Math.max(op[i - 1][j], op[i - 1][j - weight[i - 1]] + value[i - 1]); } else { op[i][j] = op[i - 1][j]; } } } return op[N][W]; }