假设ABC,三个我们一共有8个子集,分别如上图所示排列。那么我们就可以通过这个规律排列出所有的子集。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void Prinf_jihe(int n)
{
char str[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int maxnum = 1 << n; //2^n,会有多少个子集
for(int i = 0; i < maxnum; i++) //子集个数
{
for(int j = 0; j < n; j++) //每个子集二进制右数第几位
{
if((i&(1<<j)) != 0)
{
cout << str[j];
}
}
cout << endl;
}
}
int main()
{
Prinf_jihe(3);
return 0;
}