题目描述
按照字典序输出自然数 1 到 n 所有不重复的排列,即 n 的全排列,要求所产生的任一数字序列中不允许出现重复的数字。
输入格式
一个整数 nn。
输出格式
由 1∼n 组成的所有不重复的数字序列,每行一个序列。
每个数字保留 5 个场宽。
输入输出样例
输入 #1复制
3
输出 #1复制
1 2 31 3 22 1 32 3 13 1 23 2 1
说明/提示
1≤n≤9。
思路:
爆搜模版,注意#include <iomanip>是,wets(5)的头文件
代码如下:
#include<iostream>
#include <iomanip>
using namespace std;
int n;
int arr[100];
bool vis[100];
void dfs(int x)
{if(x > n){for(int i = 1 ; i <= n ; i++)cout << setw(5) << arr[i];cout << endl;return;}for(int i = 1 ; i <= n ; i++){if(vis[i] == false){vis[i] = true;arr[x] = i;dfs(x+1);vis[i] = false;arr[x] = 0; } }
}
int main(void)
{cin >> n;dfs(1);return 0;}

