SHARE
TWEET

Untitled

a guest Feb 19th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. int n, a[100];
  5. int binar(int st, int dr, int x)
  6. {
  7.     int m;
  8.     if(st==dr)
  9.         if(x==a[st])
  10.             return st;
  11.         else
  12.             return 0;
  13.     else
  14.     {
  15.         m=(st+dr)/2;
  16.         if(x==a[m])
  17.             return m;
  18.         else
  19.             if(x>a[m])
  20.                 return binar(m+1, dr, x);
  21.             else
  22.                 return binar(st, m-1, x);
  23.     }
  24.  
  25. }
  26.  
  27. int main()
  28. {
  29.     int st, dr, x, n, a[100],b;
  30.     cout<<"x= "; cin>>x;
  31.     cout<<"n= "; cin>>n;
  32.     for(int i=1; i<=n; i++)
  33.         cin>>a[i];
  34.     //int(st, dr, x);
  35.     b=binar(1, n, x);
  36.     if(b)
  37.         cout<<"da, pe pozitia "<<b;
  38.     else
  39.         cout<<"nu";
  40.     return 0;
  41. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top