AcWing 92. 递归实现指数型枚举
原题链接
简单
作者:
XXXJESSIE
,
2024-03-13 17:28:11
,
所有人可见
,
阅读 9
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int N=20;
int n;
int st[N];//记录每个数的状态 0没考虑 1 选2 不选
void dfs(int x)//x表示这个数枚举到那个位置
{
if(x>n)
{
for(int i=1;i<=n;i++)
{
if(st[i]==1)
{
printf("%d ",i);//如果选这个数,打印出来 空一格 i是st的下角标
}
}
printf("\n");
return;//例如当 x > n 时返回即可。 否则陷入死循环
}
//不选
st[x]=2;
dfs(x+1);
st[x]=0;
//choice
st[x]=1;
dfs(x+1);
st[x]=0;
}
int main()
{
scanf("%d",&n);
dfs(1);
return 0;
}