[[toc]]
题目
问题描述
有
输入样例
3
1 2 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;
const int maxn = 10;
int n;
int a[maxn];
int chs[maxn]; //选的数
bool vis[maxn]; //第i个数是否被选
void init() {
std::cin >> n;
for(int i = 1;i <= n ;++i ) // i: 1->n
{
std::cin >> a[i];
}
}
void full_permutation(int dep) {
if( dep > n) //到达边界
{
for(int i = 1;i <= n ;++i ) // i: 1->n
{
cout << chs[i] << " ";
}
cout << endl;
return;
}
for(int i =1;i<=n ;i++) {
if( !vis[i] )
{
vis[i] = 1;
chs[dep] = a[i];
full_permutation(dep+1);
vis[i] = 0;
}
}
}
int main() {
init();
full_permutation(1);
return 0;
}