背包问题

 1 package Test;
 2 import java.util.*;
 3 public class Solution {
 4     
 5     public static int jisuan(int W,int[] wei,int [] val,int n)
 6     {
 7         int i,w;
 8         int[][] K=new int[n+1][W+1];
 9         
10         
11         for(i=0;i<=n;i++)
12         {
13             for(w=0;w<=W;w++)
14             {
15                 if(i==0||w==0)
16                 {
17                     K[i][w]=0;
18                 }
19                 else if (wei[i-1]<w){
20                     
21                     K[i][w]=Math.max(val[i-1]+K[i-1][w-wei[i-1]], K[i-1][w]);
22                 }
23                 else {
24                     K[i][w] = K[i-1][w];
25                 }
26             }
27         }
28         
29 
30         return K[n][W];
31     }
32     
33     public static void main(String[] args) {
34         // TODO Auto-generated method stub
35         
36         Scanner sc = new Scanner(System.in);
37 
38         int n= sc.nextInt();
39         
40         int [] pro = new int[100];
41         int [] wei = new int[100];
42         
43         for(int i=0;i<n;i++)
44         {
45             pro[i] = sc.nextInt();
46         }
47         
48         for(int i=0;i<n;i++)
49         {
50             wei[i] = sc.nextInt();
51         }
52         
53         int time = sc.nextInt();
54         
55         System.out.println(jisuan(time,wei,pro,n));
56     }
57     
58 }

 

posted @ 2019-07-05 14:59  王爷爱吃秋刀鱼  阅读(108)  评论(0编辑  收藏  举报