Renewed Structure

This commit is contained in:
e2hang
2025-08-28 21:17:28 +08:00
parent b97f348a51
commit 1a5c4329b2
163 changed files with 312 additions and 0 deletions

View File

Binary file not shown.

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

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

Binary file not shown.

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

Binary file not shown.

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

Binary file not shown.

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

Binary file not shown.

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

Binary file not shown.

View 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);
}
*/

Binary file not shown.

Binary file not shown.

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