#C08L07P02. C08.L07.回溯.程序填空.桐桐的全排列

C08.L07.回溯.程序填空.桐桐的全排列

题目描述

今天,桐桐的老师布置了一道数学作业,要求列出所有从数字 1 到数字 n 的连续自然数的排列,要求所产生的任一数字序列中不允许出现重复的数字。因为排列数很多,桐桐害怕写漏了,所以她决定用计算机编程来解决。

输入格式

只有一个整数 n(1 ≤ n ≤ 9)。

输出格式

按字典序输出由 1 至 n 组成的所有不重复的数字序列,每行一个序列,每个数字之间有一个空格。

样例

3
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

程序填空

#include<bits/stdc++.h>
using namespace std;
int ans[10],n;
bool Lock[10];  //用于记录一个数字是否被使用过 
void dfs(int l)
{
	int i;
	if(l==n+1)
	{
		for(i=1;i<=n;i++)
			printf("%d ",ans[i]);
		printf("\n");
	}
	else
	{
		for(i=1;i<=n;i++)
		{
			if( 填空(1) ) continue;
			填空(2) ; 
			ans[l] = i;
			dfs( 填空(3) );
			填空(4) ;
		}
	}	
}

int main()
{
	cin>>n;
	
	dfs( 填空(5) );

	return 0;
}

填空(1):{{ input(1) }}

填空(2):{{ input(2) }}

填空(3):{{ input(3) }}

填空(4):{{ input(4) }}

填空(5):{{ input(5) }}