o1背包的动态规划算法

public class 背包01Test {
     
	public static void knapsack(int[] v,int[] w,int c,int[][] m) {
		int n = v.length -1;
		int jmax = Math.min(w[n] - 1, c);
		for(int j = 0;j<=jmax;j++)
			m[n][j] = 0;
		for(int j = w[n];j<=c;j++)
			m[n][j] = v[n];
		
		for(int i = n-1;i>1;i--) {
			jmax = Math.min(w[i]-1, c);
			for(int j = 0;j<=jmax;j++)
				m[i][j] = m[i+1][j];
			for(int j = w[i];j<=c;j++)
				m[i][j] = Math.max(m[i+1][j], m[i+1][j-w[i]]+v[i]);
		}
		m[1][c]=m[2][c];
		if(c>=w[1])
			m[1][c] = Math.max(m[1][c], m[2][c-w[1]]+v[1]);
	}
	
	
	public static void braceback(int[][] m,int[] w,int c,int[] x) {
		
		int n = w.length-1;
		for(int i = 0;i0)?1:0;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		System.out.println("请输入最多承受的容量C:");
		int c = input.nextInt();
		System.out.println("输入物品的个数:");
		int n = input.nextInt();
		int[] w = new int[n+1];
		int[] v = new int[n+1];
		System.out.println("清输入w[]:");
		int i = 1;
		while(i
		for(int a = 1;a

代码如上


运行结果如下

o1背包的动态规划算法_第1张图片

你可能感兴趣的:(o1背包的动态规划算法)