Advertisement
STANAANDREY

matrix_search(sorted)

Jul 3rd, 2019
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool matrix_src(int n,int m,int a[500][500],int x)
  5. {
  6. int i=0,j=n-1;
  7. while (i<n&&j>=0)
  8. {
  9.     if (x==a[i][j])
  10.     return true;
  11.     else if (x<a[i][j])
  12.         j--;
  13.     else
  14.         i++;
  15. }
  16.  return false;
  17. }
  18. int main()
  19. {
  20.     int a[500][500],n,m,i,j,k,v[5000];
  21.     cin>>n>>m;
  22.     for (i=0; i<n; i++)
  23.         for (j=0; j<m; j++)
  24.             cin>>a[i][j];
  25.     cin>>k;
  26.     for (i=0; i<k; i++)
  27.         cin>>v[i];
  28.     for (i=0; i<k; i++)
  29.     {
  30.         if (matrix_src(n,m,a,v[i]))
  31.             cout<<"DA"<<'\n';
  32.         else
  33.             cout<<"NU"<<'\n';
  34.     }
  35.  
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement