codevs1294 全排列
题目描述 Description
给出一个n, 请输出n的所有全排列
输入描述 Input Description
读入仅一个整数n (1<=n<=10)
输出描述 Output Description
一共n!行,每行n个用空格隔开的数,表示n的一个全排列。并且按全排列的字典序输出。
样例输入 Sample Input
3
样例输出 Sample Output
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
#include<cstdio>
using namespace std;
bool b[15];
int n,a[15];
void DFS(int x){
if(x==n+1){
for(int i=1;i<=n;i++)printf("%d ",a[i]);
printf("\n");
return;
}
for(int i=1;i<=n;i++)if(!b[i]){
b[i]=true; a[x]=i; DFS(x+1); a[x]=0; b[i]=false;
}
}
int main(){
scanf("%d",&n);
DFS(1);
return 0;
}

浙公网安备 33010602011771号