// by BNU_LZM
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1010;
int a[maxn], s[maxn];
void print_set1(int n, int *S, int cur)
{
for(int i = 1; i <= cur; i++) printf("%d ", a[S[i]]);
printf("\n");
int q;
if(cur == 0) q = 1;
else q = S[cur]+1;
for(int i = q; i <= n; i++)
{
S[cur+1] = i;
print_set1(n, S, cur+1);
}
}
void print_set2(int n, int *S, int cur)
{
if(cur == n)
{
for(int i = 1; i <= n; i++) if(S[i]) printf("%d ", a[i]);
printf("\n");
}
else
{
S[cur+1] = 0;
print_set2(n, S, cur+1);
S[cur+1] = 1;
print_set2(n, S, cur+1);
}
}
void print_set3(int n, int x)
{
for(int i = 1; i <= n; i++)
{
if((1<<(i-1)) & x) printf("%d ", a[i]);
}
printf("\n");
}
int main()
{
freopen("in.txt", "r", stdin);
int n;
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
sort(a+1, a+n+1);
int cur = 0;
//print_set1(n, s, cur);//增量法
//print_set2(n, s, cur);//位向量法
for(int i = 0; i < (1<<n); i++)//二进制法
{
print_set3(n, i);
}
return 0;
}
生成子集
最新推荐文章于 2021-03-24 21:15:04 发布