Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*sa se scrie un program ce foloseste un subprog. recursiv cu metoda DEI ce verifica daca un element x se gaseste intr-o matrice*/
- #include <iostream>
- #define NMAX 100
- using namespace std;
- void readVar(char varname, int &var) {
- cout << varname << '=';
- cin >> var;
- }
- void readMat(int &n, int &m, int a[][NMAX]) {
- readVar('n', n);
- readVar('m', m);
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++)
- cin >> a[i][j];
- }
- int findInV(int v[], int p, int q, int x) {
- if (p > q)
- return 0;
- int mid = (p + q) / 2;
- return findInV(v, p, mid - 1, x) || findInV(v, mid + 1, q, x) || (v[mid] == x);
- }
- int findInM(int a[][NMAX], int p, int q, int x, int m) {
- if (p > q)
- return 0;
- int mid = (p + q) / 2;
- return findInM(a, p, mid - 1, x, m) || findInM(a, mid + 1, q, x, m) || findInV(a[mid], 0, m - 1, x);
- }
- int n, m, a[NMAX][NMAX], x;
- int main() {
- readMat(n, m, a);
- readVar('x', x);
- cout << findInM(a, 0, n - 1, x, m) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement