Advertisement
Norbysweg

T50

Feb 3rd, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int a[20],n,x,k;
  8.     cout<<"n=";
  9.     cin>>n;
  10.     cout<<"x=";
  11.     cin>>x;
  12.     for (int i=0;i<n;i++)
  13.     {
  14.         cout<<"a["<<i<<"]=";
  15.         cin>>a[i];
  16.     }
  17.     int i=0;
  18.     while (i<n)
  19.     {
  20.         k=(i+n-1)/2;
  21.         if (a[k]==x)
  22.         {
  23.             cout<<"van";
  24.             return 0;
  25.         }
  26.         else
  27.         {
  28.             if (x>a[k])
  29.             {
  30.                 i=k+1;
  31.             }
  32.             else
  33.             {
  34.                 n=k-1;
  35.             }
  36.         }
  37.     }
  38.     if (i>=n)
  39.     {
  40.         cout<<"nem volt";
  41.     }
  42.  
  43.     return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement