-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4_FullPermutation.cpp
More file actions
65 lines (58 loc) · 1.43 KB
/
4_FullPermutation.cpp
File metadata and controls
65 lines (58 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* P115 全排列 递归
*/
#include <iostream>
#define MAXN 10
using namespace std;
int n;
int permutationArray[MAXN];
bool hashTable[MAXN] = {false};
void fullPermutationFunc(int index)
{
// 当 permutationArray 有 n 个元素了,结束递归
// index == n + 1. 递归边界,已经处理完前 1~n 位
if (index == n + 1)
{
// print
for (int i = 0; i < n; i++)
{
cout << permutationArray[i + 1];
}
cout << endl;
return;
}
// 还没有处理完
else
{
// 循环检查,是否已经放在了 permutaionArray 中
for (int i = 1; i <= n; i++)
{
// 在 permutationArray 中,继续循环
// 不在 permutaionArray 中
if (hashTable[i] == false)
{
// 加入到 permutationArray 中
permutationArray[index] = i;
// 设置为 true
hashTable[i] = true;
// 递归执行 index 后面的排序
fullPermutationFunc(index + 1);
// 执行完后,清除 index 位,进行下一个数的全排列
hashTable[i] = false;
}
}
}
}
int main()
{
cout << "Input n: ";
cin >> n;
while (n >= MAXN)
{
cout << "Too big. Input again: ";
cin >> n;
}
// 从1开始
fullPermutationFunc(1);
return 0;
}