Renewed Structure
This commit is contained in:
0
Algorithm/Recursion/0Resursion 递归
Normal file
0
Algorithm/Recursion/0Resursion 递归
Normal file
BIN
Algorithm/Recursion/P27_All_Sorted
Normal file
BIN
Algorithm/Recursion/P27_All_Sorted
Normal file
Binary file not shown.
38
Algorithm/Recursion/P27_All_Sorted.cpp
Normal file
38
Algorithm/Recursion/P27_All_Sorted.cpp
Normal file
@@ -0,0 +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;
|
||||
}
|
||||
12
Algorithm/Recursion/P28_19_n!.cpp
Normal file
12
Algorithm/Recursion/P28_19_n!.cpp
Normal file
@@ -0,0 +1,12 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int jc(int n){
|
||||
if(n > 0)
|
||||
return jc(n - 1)*n;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main(){
|
||||
cout << jc(10) << endl;
|
||||
return 0;
|
||||
}
|
||||
BIN
Algorithm/Recursion/P28_19_n!.exe
Normal file
BIN
Algorithm/Recursion/P28_19_n!.exe
Normal file
Binary file not shown.
15
Algorithm/Recursion/P28_20_Feb.cpp
Normal file
15
Algorithm/Recursion/P28_20_Feb.cpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int feb(int n){
|
||||
if(n > 1)
|
||||
return feb(n - 1) + feb(n - 2);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main(){
|
||||
for(int i = 1;i < 10; i++){
|
||||
cout << feb(i) << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
BIN
Algorithm/Recursion/P28_20_Feb.exe
Normal file
BIN
Algorithm/Recursion/P28_20_Feb.exe
Normal file
Binary file not shown.
14
Algorithm/Recursion/P29_21_ComplexFunc.cpp
Normal file
14
Algorithm/Recursion/P29_21_ComplexFunc.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int cf(int n){
|
||||
if(n%2 == 1)
|
||||
return cf(3*n + 1);
|
||||
if(n%2 == 0)
|
||||
return n / 2;
|
||||
}
|
||||
|
||||
int main(){
|
||||
cout << cf(5) << " "<< cf(6) << " " << cf(7) << endl;
|
||||
return 0;
|
||||
}
|
||||
BIN
Algorithm/Recursion/P29_21_ComplexFunc.exe
Normal file
BIN
Algorithm/Recursion/P29_21_ComplexFunc.exe
Normal file
Binary file not shown.
21
Algorithm/Recursion/P29_22_Ackermann.cpp
Normal file
21
Algorithm/Recursion/P29_22_Ackermann.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
|
||||
int ack(int i, int j){
|
||||
if(i == 1 && j >= 1)
|
||||
return pow(2,j);
|
||||
if(i >=2 && j == 1)
|
||||
return ack(i - 1, 2);
|
||||
if(i >= 2 && j >= 2)
|
||||
return ack(i - 1, ack(i, j - 1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(){
|
||||
cout << "1,2: " << ack(1,2) << endl;
|
||||
cout << "2,1: " << ack(2,1) << endl;
|
||||
cout << "2,2: " << ack(2,2) << endl;
|
||||
cout << "1,3: " << ack(1,3) << endl;
|
||||
return 0;
|
||||
}
|
||||
BIN
Algorithm/Recursion/P29_22_Ackermann.exe
Normal file
BIN
Algorithm/Recursion/P29_22_Ackermann.exe
Normal file
Binary file not shown.
20
Algorithm/Recursion/P29_23_GCD.cpp
Normal file
20
Algorithm/Recursion/P29_23_GCD.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
|
||||
int gcd(int x, int y){
|
||||
x = abs(x);
|
||||
y = abs(y);
|
||||
if(y == 0)
|
||||
return x;
|
||||
if(y > 0)
|
||||
return gcd(y, x % y);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(){
|
||||
cout << gcd(20 ,30) << endl;
|
||||
cout << gcd(112, 42) << endl;
|
||||
cout << gcd(-112,42) << endl;
|
||||
return 0;
|
||||
}
|
||||
BIN
Algorithm/Recursion/P29_23_GCD.exe
Normal file
BIN
Algorithm/Recursion/P29_23_GCD.exe
Normal file
Binary file not shown.
33
Algorithm/Recursion/P29_24_checkfunc.cpp
Normal file
33
Algorithm/Recursion/P29_24_checkfunc.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
Request: Code a template module to check if x is in a[0:n-1]
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
template <class T>
|
||||
bool chk(T x, T* a, int n){
|
||||
if(n > 1 && x != a[n - 1])
|
||||
return chk(x,a,n-1);
|
||||
if(n == 1 && x != a[0])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(){
|
||||
int a[] = {2,3,4,5,6,7,9};
|
||||
cout << chk(2,a,7) << " " << chk(1,a,7)<<endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
AI wrote like this:
|
||||
|
||||
template <class T>
|
||||
bool chk(T x, T* a, int n) {
|
||||
if (n <= 0) return false;
|
||||
if (a[n - 1] == x) return true;
|
||||
return chk(x, a, n - 1);
|
||||
}
|
||||
*/
|
||||
BIN
Algorithm/Recursion/P29_24_checkfunc.exe
Normal file
BIN
Algorithm/Recursion/P29_24_checkfunc.exe
Normal file
Binary file not shown.
BIN
Algorithm/Recursion/P29_25_SubsetGen
Normal file
BIN
Algorithm/Recursion/P29_25_SubsetGen
Normal file
Binary file not shown.
51
Algorithm/Recursion/P29_25_SubsetGen.cpp
Normal file
51
Algorithm/Recursion/P29_25_SubsetGen.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
//Generate subset
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
//m: 本次递归选原数组第几个
|
||||
//n: 本次递归数组长度
|
||||
//l: 给定数组长度
|
||||
template <class T>
|
||||
T* sub(int m ,int n, int l, T* x, T* tmp){
|
||||
if(m == l)
|
||||
{
|
||||
|
||||
cout << "[ ";
|
||||
for(int i = 0; i < n; i++){
|
||||
cout << tmp[i] << " ";
|
||||
}
|
||||
cout << " ]" << endl;
|
||||
return tmp;
|
||||
|
||||
}
|
||||
//不选a[i]
|
||||
sub(m + 1, n, l, x, tmp);
|
||||
|
||||
//选a[i]
|
||||
tmp[n] = x[m];
|
||||
sub(m + 1, n + 1, l, x, tmp);
|
||||
|
||||
|
||||
}
|
||||
|
||||
int main(){
|
||||
//cin >> n;
|
||||
int n = 5;
|
||||
int num = pow(2 ,n);
|
||||
double** p;
|
||||
p = new double* [num];
|
||||
for(int i = 0; i < num ; i++){
|
||||
p[i] = new double[n];
|
||||
}
|
||||
double* tmp = new double[n];
|
||||
double arr[] = {1, 2, 3, 4, 5};
|
||||
sub(0, 0, 5, arr, tmp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user