输出1——n的排列(深度优先搜索)

时间:2024-03-22 19:35:44

样例输入:

3

样例输出:

123
132
213
231
312
321

 #include <stdio.h>
int n; void dfs(int step,int* a,int* book)
{
int i;
if(step == n)
{
for(i = ;i < n;i++)
{
printf("%d",a[i]);
}
printf("\n");
return ;
} for(i = ;i < n;i++)
{
if(book[i] == )
{
a[step] = i+;
book[i] = ;
dfs(step+,a,book);
book[i] = ;
}
}
}
int main()
{
scanf("%d",&n);
int a[n+],book[n+],i;
for(i = ;i < n;i++)
{
a[i] = ;
book[i] = ;
}
dfs(,a,book);
return ;
}