首页 > 编程语言 > 详细

Java递归版01背包

时间:2019-10-05 09:25:16      阅读:91      评论:0      收藏:0      [点我收藏+]
class Solutionx{
    private int memo[][];
    private int w[];
    private int v[];
    
    public int bestValue(int Index,int c){
         if(Index < 0||c < 0){
            return 0;
         }       
         if(memo[Index][c]!=-1){
         return memo[Index][c];
         }
         int res=bestValue(Index-1,c);
         if(c>=w[Index]){
          res=Math.max(res, bestValue(Index-1,c-w[Index])+v[Index]);
         }
         memo[Index][c]=res;
         return res;
    }
    public int kcap(){
    
       w=new int[]{0,4,2,3,5};
       v=new int[]{0,2,5,1,4};
       memo=new int[8][8];
       for(int i=0;i<8;++i){
         for(int j=0;j<8;++j){
         memo[i][j]=-1;
         }
       }
       return bestValue(4,7); 
   }
 }
 
 
 
 
 public class Mainx {
    public static void main(String[] args) {
        Solutionx space=new Solutionx();
        System.out.println(space.kcap());
    }
 }
 

 

Java递归版01背包

原文:https://www.cnblogs.com/z2529827226/p/11623813.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!