Files
Data-Structure/Algorithm/Recursion/P29_24_checkfunc.cpp
2025-08-28 21:17:28 +08:00

34 lines
558 B
C++

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