Advertisement
Aveneid

Untitled

Jan 9th, 2018
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <cmath>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. ifstream odczyt("dane.txt");
  8. ofstream zapis("wynik.txt");
  9.  
  10. int p_lin( int tab[], int n, int x )
  11. {
  12.     int ile_x=0;
  13.  
  14.     for( int i=0; i<n; i++ )
  15.     {
  16.         if( tab[i] == x )
  17.             ile_x++;
  18.     }
  19.  
  20.     return ile_x;
  21. }
  22.  
  23. int p_bin(int tab[], int a, int b, int y, bool r, bool m )
  24. {
  25.     int l, p, s, in=-1, ile_x=1;
  26.  
  27.     if( a<=b ) { l=a; p=b; }
  28.     else { l=b; p=a; }
  29.  
  30.     while (l<=p)
  31.     {
  32.         s=(l+p)/2;
  33.         if (tab[s]==y)
  34.         {
  35.             in = s;
  36.             break;
  37.         }
  38.         else
  39.         {
  40.             if( r )
  41.             {
  42.                 if (tab[s]<y)
  43.                     l=s+1;
  44.                 else
  45.                     p=s-1;
  46.             }
  47.             if( m )
  48.             {
  49.                 if (tab[s]>y)
  50.                     l=s+1;
  51.                 else
  52.                     p=s-1;
  53.             }
  54.         }
  55.     }
  56.  
  57.     if( in==-1 )
  58.         return 0;
  59.     else
  60.     {
  61.         for( int i=in+1; i<=b; i++ )
  62.             if( tab[i]==y ) ile_x++;
  63.             else break;
  64.  
  65.         for( int i=in-1; i>=a; i-- )
  66.             if( tab[i]==y ) ile_x++;
  67.             else break;
  68.  
  69.         return ile_x;
  70.     }
  71. }
  72.  
  73.  
  74.  
  75. using namespace std;
  76.  
  77. int main()
  78. {
  79.     int n, x, *t;
  80.     bool mal=true, ros=true;
  81.  
  82.     odczyt >> n >> x;
  83.     t = new int[n+1];
  84.     odczyt >> t[0];
  85.  
  86.     for( int i=1; i<n; i++ )
  87.     {
  88.         odczyt >> t[i];
  89.  
  90.         if( t[i]>t[i-1] )
  91.             mal = false;
  92.         if( t[i]<t[i-1] )
  93.             ros = false;
  94.     }
  95.  
  96.     if( mal==ros )
  97.         zapis << "NIE " << p_lin( t, n, x );
  98.     else
  99.         zapis << "TAK " << p_bin( t, 0, n, x, ros, mal );
  100.  
  101.     delete [] t;
  102.     odczyt.close();
  103.     zapis.close();
  104.     system("pause");
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement