使用位掩码:
int allMasks = (1 << N);for (int i = 1; i < allMasks; i++){ for (int j = 0; j < N; j++) if ((i & (1 << j)) > 0) //The j-th element is usedSystem.out.print((j + 1) + " "); System.out.println();}这是所有位掩码:
1 = 001 = {1}2 = 010 = {2}3 = 011 = {1, 2}4 = 100 = {3}5 = 101 = {1, 3}6 = 110 = {2, 3}7 = 111 = {1, 2, 3}您知道二进制的第一位是最右边的。



