1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#include <iostream>
int FindI(int key, const int B[], int M, int j) {
for (int i = 0; i < M; ++i)
if (B[i] == key)
return j + 2 * i;
return -1;
}
int FindR(int key, const int B[], int M, int j) {
if (M < 0)
return M;
if (B[M - 1] == key)
return j + 2 * (M - 1);
return FindR(key, B, M - 1, j);
}
int main()
{
const int arr[] {1, 2, 3, 4, 5};
std::cout << FindI(2, arr, 5, 4) << '\n';
std::cout << FindI(6, arr, 5, 4) << '\n';
std::cout << FindR(2, arr, 5, 4) << '\n';
std::cout << FindR(6, arr, 5, 4) << '\n';
}
|