집합된 모든 서브집합 구하기 (java 구현)
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
/*
*Created on 2015 1 28
*Copyright 2015 Y. Cai Limited crop. All Rights Reserved
*
*7289***[email protected]
*/
public class Subset {
public static void getSubset(int set[]) throws IOException
{
FileWriter writer = new FileWriter(new File("src/Result2.txt"));
int length=set.length;//
int num=(2<<set.length-1)-1;//
for(int i=1;i<=num;i++)
{
System.out.print("[");
int now=i; // i
for(int j=0;j<=length-1;j++)
{
if((now&1)==1) // 1
{
System.out.print(set[j]);
writer.write("" + set[j] + " ");
}
now=now>>1; //
}
writer.write("\r
");
System.out.print("]");
}
writer.close();
}
public static void main(String[] args) throws IOException {
int set[]={1,2,3,4};
getSubset(set);
}
}
실행 결과: