求一个集合的所有子集 输出一个数所有平方和的情况 背包问题的递归解决

来源:互联网 发布:宜兴俊知集团 编辑:程序博客网 时间:2024/05/29 07:07

1、解决这个问题无论采用那种方法首先需要确定的是:对于集合里面的任何一个元素,有两种可能,一种是在子集合里,另一种是不在子集合里。
对此收现金po出的是理解得比较久的代码
public static void subsets(List<String> list) {///增量构造法    subsets(list, 0, list.size());}
public static void subsets(List<String> list, int start, int end) {    if (end == 0) {        System.out.println(list);    } else {        subsets(list, start+1, end - 1); ///递归求包含第一个元素的所有子集(从start+1起的集合的子集都包括第一个元素的情况)              String word = list.remove(start);///删除第一个元素        subsets(list, start, end - 1);///递归求除去第一个元素的所有子集(递归求不包括第一个元素的所有子集)        list.add(start, word);///最终还原删掉的元素    }}
这道题对应的是practice it:https://practiceit.cs.washington.edu/problem/view/bjp3/chapter12/e20-subsets,它题目要求不使用任何循环来实现这个问题的解决。想了挺久的,想不出来,这是在github 复制下来的代码。之所以第一个递归式子是start+1是因为假设了第一个元素默认存在于后面的集合的任意一个子集中。第二个式子就把第一个元素除掉,用同样的方式递归得出除去第一个元素的集合的所有子集。

2、题目:https://practiceit.cs.washington.edu/problem/view/bjp3/chapter12/e21-maxSum(背包问题的递归解决)
public static int maxSum(List<Integer> L,int limit){    int k=solve(0,limit,L);    //System.out.println("L"+L.size());    return k;}public static int solve(int st,int limit,List<Integer> L){    if(st==L.size())        return 0;///要把判断放在前面,不然会re    if(L.get(st)<=limit)        return Math.max(solve(st+1,limit,L),solve(st+1,limit-L.get(st),L)+L.get(st));            return solve(st+1,limit,L);}

3、
import java.util.*;void printSquares(int n){    ArrayList<Integer> a=new ArrayList<Integer>();    int m=(int)Math.sqrt(n)+1;///确定可能出现的数字最多有多少    int[] num=new int[m+1];    int now=0;    for(int i=1;i<num.length;i++){        num[i]=i;///把数字先存到数组中    }    compute(n,1,a,num,now);}boolean compute(int n,int sindex,ArrayList<Integer> a,int[] num,int now){            if(now==n){                printHelper(a);                return true;            }///如果等于那个数则返回true            if(now>n)                return false;///否则false    for(int i=sindex;i<num.length;i++){        if(num[i]!=-1){///这个数没有出现过            int temp=num[i];            num[i]=-1;///数出现过则设置为-1            a.add(temp);///暂时存储进a            compute(n,i+1,a,num,now+temp*temp);            a.remove(a.size()-1);///因为a是公共的存储空间,所以要清空            num[i]=temp;///还原数组,准备第二组数据        }    }    return false;}


原创粉丝点击