有一个含n个整数的数组a,所有元素均不相同,设计一个算法求其所有子集(幂集)。输出:求解结果{} {3}{2}{23}{1}{13}{12}{123}
public static void main(String[] args) { int a[] = {1,2,3};
int n = 3;
int x[] = new int [n];
dfs(a,n,0,x);
}
public static void dfs(int a[],int n,int i ,int x[]) {
if(i>=n) {
printResult(a,n,x);
}
else {
x[i] = 0;
dfs(a,n,i+1,x);
x[i] = 1;
dfs(a,n,i+1,x);
}
}
public static void printResult(int a[],int n,int x[]){
int i ;
System.out.print("{");
for(i =0;i<n;i++) {
if(x[i]==1) {
System.out.print(a[i]);
}
}
System.out.print("}");
System.out.println("");
}
这样会使得每次输出的结果都不一样
标签:幂集,int,void,dfs,static,回溯,子集,public From: https://blog.csdn.net/qq_62691586/article/details/144516202