用到的是状态压缩,一直想学状态压缩的..
姥姥滴.老是学不会.
这次是懂了百分之80%
知道了为什么用&了...
是这样的,
把你要生成子集的数,
按照
a0 a1 a2 a3
0 0 0 0 // 状态栏
1 3 5 6 // 数组拦
出现1的时候就输出对应的书阻拦的数就可以了.
爱情保卫战,怎么那么搞笑- -#
贴出代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
/*
这就是传说的状态压缩..FUCK..
a0 a1 a2 a3 a4 // 数组序号
0 0 0 0 0 // 数组的状态(只有1和0)
1 5 7 8 10 // 数组值
然后通过 0 代表没有选, 1 代表原则上;
则可以ok
*/
using namespace std;
const int maxn = 1111;
int a[maxn];
void Print_subset(int n, int s)
{
for (int i = 0; i < n; i++)
{
if (s & (1 << i))
{
printf("%d", a[i]);
}
}
cout << endl;
}
int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
for (int i = 0; i < (1 << n); i++)
{
Print_subset(n, i);
}
}
system("pause");
return 0;
}