【问题描述】
输出自然数1到n所有不重复的排列,即n的全排列,要求所产生的任一数字序列中不允许出现重复的数字。
【输入格式】
n(1≤n≤9)
【输出格式】
由1~n组成的所有不重复的数字序列,每行一个序列。
【输入样例】Form.in
3
【输出样例】Form.out
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
#include<iostream>
#include<cstdio>
#include<cstring>
#include<
string>
#include<algorithm>
#include<cmath>
using namespace std;
int n,a[
1000]={
1};
bool b[
1000];
void dfs(
int);
void print();
int main()
{
cin>>
n;
dfs(1);
return 0;
}
void dfs(
int q)
{
int i;
for(i=
1;i<=n;i++
)
{
if(!
b[i])
{
a[q]=
i;
b[i]=
1;
if(q<n)dfs(q+
1);
else print();
b[i]=
0;
}
}
}
void print()
{
for(
int i=
1;i<=n;i++
)
{
cout<<a[i]<<
" ";
}
cout<<
endl;
}
转载于:https://www.cnblogs.com/sssy/p/6611287.html
相关资源:全排列代码,C语言代码,用来解决全排列问题