#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_set>
#include <set>
using namespace std;
int p[3]={1,2,3};//p[n],
unordered_set<int> S;
int main()
{
for (int i = 1; i < 1<<3; i ++ )//循环次数 1<<n
{
int s = 0;
for (int j = 0; j < 3; j ++ )//j<n
if (i >> j & 1)//求出所有子集
s += p[j];
S.insert(s);
}
for (unordered_set<int>::iterator it = S.begin(); it != S.end(); ++it)
printf("%d\n", *it);
return 0;
}
···
输出:
0
6
2
1
3
4
5
···