1. 程式人生 > >生成子集(Java)

生成子集(Java)

package suanfa;

public class ziji {
	public static void main(String[] args) {
		int str[] = { 1, 2, 3 };
		int length = 1 << str.length;
		for (int i = 0; i < length; i++) {
			print(str, i);
		}
	}

	public static void print(int[] str, int i) {
		int N = str.length;
		for (int j = 0; j < N; j++) {
			if ((i & (1 << j)) != 0) {
				System.out.print(str[j] + " ");
			}
		}
		System.out.println();
	}
}