标签:台阶 == 初始 sys class target rate 完全背包 pre
01背包
package 背包; public class 背包 { public static void main(String[] args) { // TODO Auto-generated method stub int[] w= {3,5,2,6,4}; int[] val= {4,4,3,5,3}; int m=12;//容量 int n=val.length; int[] f=new int[m+1];//价值 //不必装满,初始化为0 for(int i=0;i<f.length;i++) { f[i]=0; } for(int i=0;i<n;i++) { for(int j=m;j>=w[i];j--) { f[j]=Math.max(f[j], f[j-w[i]]+val[i]); } } System.out.println(f[m]); } }
完全背包
多重背包
混合背包
二维费用背包
分组背包
背包问题求方案数
求背包问题方案
有依赖的背包问题
跳台阶
f(1) = 1, f(2) = 2, f(3) = 3, f(4) = 5,
f(n) = f(n-1) + f(n-2)
public class Solution { public int JumpFloor(int target) { if(target==0)return 0; if(target==1)return 1; if(target==2)return 2; int pre=1; int cur=2; int next=0; for(int i=3;i<=target;i++){ next=pre+cur; pre=cur; cur=next; } return next; } }
矩形覆盖
f(n) = f(n-1) + f(n-2)
public class Solution { public int RectCover(int target) { int sum=0; int a=1; int b=2; if(target==1) return 1; if(target==2) return 2; for(int i=3;i<=target;i++) { sum=a+b; a=b; b=sum; } //若用递归方法:横着放 target-1 种方法 竖着放 target-2方法 return sum; } }
变态跳台阶
f(1) = 1
f(2) = f(2-1) + f(2-2) //f(2-2) 表示2阶一次跳2阶的次数。
f(3) = f(3-1) + f(3-2) + f(3-3)
...
f(n) = f(n-1) + f(n-2) + f(n-3) + ... + f(n-(n-1)) + f(n-n)
| 1 ,(n=0 )
f(n) = | 1 ,(n=1 )
public class Solution { public int JumpFloorII(int target) { if (target==0) return 0; if(target==1)return 1; if(target==2)return 2; int pre=2; int cur=0; // int next=0; for(int i=3;i<=target;i++){ cur=2*pre; pre=cur; } return cur; } }
标签:台阶 == 初始 sys class target rate 完全背包 pre
原文地址:https://www.cnblogs.com/NeverGiveUp0/p/11136888.html