Recursion Final
This commit is contained in:
@@ -1,9 +1,38 @@
|
||||
//For a,b,c we have sort abc acb bac bca cab cba(6 kinds) This Program is to sort all of the kinds
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
// 递减n叉树
|
||||
// 可能需要包展开,这里先不写,先以3个为主
|
||||
template<typename T>
|
||||
T* sort(int n, int l, T* tmp, bool* used, T* arr){
|
||||
if(n == l){
|
||||
cout << "[";
|
||||
for(int i = 0;i < n; i++){
|
||||
cout << tmp[i] <<" ";
|
||||
}
|
||||
cout << "]" << endl;
|
||||
}
|
||||
|
||||
for(int i = 0;i < l; i++){
|
||||
if(used[i] == false){
|
||||
tmp[n] = arr[i];
|
||||
used[i] = true;
|
||||
sort(n + 1, l, tmp, used, arr);
|
||||
used[i] = false; //是否可以删除?不可以,回溯本节点要用
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
int* tmp = new int[n];
|
||||
int* arr = new int[n];
|
||||
for(int i = 0;i < n; i++){
|
||||
cin >> arr[i];
|
||||
}
|
||||
bool* used = new bool[n];
|
||||
sort(0, n, tmp, used, arr);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user