Recursion Final

This commit is contained in:
e2hang
2025-07-18 09:32:21 +01:00
parent 8d7eb8c99c
commit ae3f0aecbb
4 changed files with 44 additions and 11 deletions

View File

@@ -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;
}
}