This commit is contained in:
e2hang
2025-07-17 19:04:50 +08:00
parent 8f1dabc334
commit d36eea5453
4 changed files with 52 additions and 0 deletions

View File

@@ -1 +1,20 @@
#include <iostream> #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
Recursion/P29_23_GCD.exe Normal file

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.