Advertisement
STANAANDREY

mat dei

Oct 12th, 2020 (edited)
1,927
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. /*sa se scrie un program ce foloseste un subprog. recursiv cu metoda DEI ce verifica daca un element x se gaseste intr-o matrice*/
  2. #include <iostream>
  3. #define NMAX 100
  4. using namespace std;
  5.  
  6. void readVar(char varname, int &var) {
  7.     cout << varname << '=';
  8.     cin >> var;
  9. }
  10.  
  11. void readMat(int &n, int &m, int a[][NMAX]) {
  12.     readVar('n', n);
  13.     readVar('m', m);
  14.     for (int i = 0; i < n; i++)
  15.         for (int j = 0; j < m; j++)
  16.             cin >> a[i][j];
  17. }
  18.  
  19. int findInV(int v[], int p, int q, int x) {
  20.     if (p > q)
  21.         return 0;
  22.     int mid = (p + q) / 2;
  23.     return findInV(v, p, mid - 1, x) || findInV(v, mid + 1, q, x) || (v[mid] == x);
  24. }
  25.  
  26. int findInM(int a[][NMAX], int p, int q, int x, int m) {
  27.     if (p > q)
  28.         return 0;
  29.     int mid = (p + q) / 2;
  30.     return findInM(a, p, mid - 1, x, m) || findInM(a, mid + 1, q, x, m) || findInV(a[mid], 0, m - 1, x);
  31. }
  32.  
  33. int n, m, a[NMAX][NMAX], x;
  34. int main() {
  35.     readMat(n, m, a);
  36.     readVar('x', x);
  37.     cout << findInM(a, 0, n - 1, x, m) << endl;
  38.     return 0;
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement