7-17-25
This commit is contained in:
@@ -1 +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
Recursion/P29_23_GCD.exe
Normal file
BIN
Recursion/P29_23_GCD.exe
Normal file
Binary file not shown.
33
Recursion/P29_24_checkfunc.cpp
Normal file
33
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
Recursion/P29_24_checkfunc.exe
Normal file
BIN
Recursion/P29_24_checkfunc.exe
Normal file
Binary file not shown.
Reference in New Issue
Block a user