2.递归回溯求子集和数问题
public class Zi { static int[] w = {1,2,2,4,5,6,6}; static int[] x = new int[w.length]; static int m = 10; static int sum=0; public static void main(String[] args) { for(int i=0;i<w.length;i++){ sum+=w[i]; } Sumofsub(0, 0, sum); } public static void Sumofsub(int s,int k,int r){ x[k]=1; if(s+w[k]==m){ for(int j=0;j<w.length;j++){ System.out.print(x[j]+" "); } System.out.println(); }else if(s+w[k]+w[k+1]<=m){ Sumofsub(s+w[k],k+1,r-w[k]); } if(s+r-w[k]>=m&&s+w[k+1]<=m){ x[k]=0; Sumofsub(s,k+1,r-w[k]); } } }
在平凡中坚持前行,总有一天,会遇见优秀的自己