编辑代码

#include <stdio.h>
 public static int func(int[] val, int[] weight, int W) {
        int N = weight.length;   
        int[][] V = new int[N + 1][W + 1]; 
        for (int col = 0; col <= W; col++) {
            V[0][col] = 0;
        }
        for (int row = 0; row <= N; row++) {
            V[row][0] = 0;
        }
        for (int i = 1; i <= N; i++) {  
            for (int j = 1; j <= W; j++) {  
                if (weight[i - 1] <= j) {  
                  
                    V[i][j] = Math.max(val[i-1] + V[j-1][j - weight[i-1]],V[i-1][j]);
                } else { 
                    V[i][j]=V[i-1][j];  
                }
            }
        }

        for (int[] rows: V) {
            for (int col : rows) {
                System.out.format("%5d",col);
            }
            System.out.println();
        }
        return V[N][W];

    }
int main(int argc, char const* argv[])
{
	
	int num = 5;
	int weight[5] = {65, 421};
	int valueAll[5] = {53532};
	bagProblem(0, weight, valueAll);
	cout << "最终的结果是:" << value << endl;
	cout << endl;
	return 0;

}