Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <cmath>
- #include <fstream>
- using namespace std;
- ifstream odczyt("dane.txt");
- ofstream zapis("wynik.txt");
- int p_lin( int tab[], int n, int x )
- {
- int ile_x=0;
- for( int i=0; i<n; i++ )
- {
- if( tab[i] == x )
- ile_x++;
- }
- return ile_x;
- }
- int p_bin(int tab[], int a, int b, int y, bool r, bool m )
- {
- int l, p, s, in=-1, ile_x=1;
- if( a<=b ) { l=a; p=b; }
- else { l=b; p=a; }
- while (l<=p)
- {
- s=(l+p)/2;
- if (tab[s]==y)
- {
- in = s;
- break;
- }
- else
- {
- if( r )
- {
- if (tab[s]<y)
- l=s+1;
- else
- p=s-1;
- }
- if( m )
- {
- if (tab[s]>y)
- l=s+1;
- else
- p=s-1;
- }
- }
- }
- if( in==-1 )
- return 0;
- else
- {
- for( int i=in+1; i<=b; i++ )
- if( tab[i]==y ) ile_x++;
- else break;
- for( int i=in-1; i>=a; i-- )
- if( tab[i]==y ) ile_x++;
- else break;
- return ile_x;
- }
- }
- using namespace std;
- int main()
- {
- int n, x, *t;
- bool mal=true, ros=true;
- odczyt >> n >> x;
- t = new int[n+1];
- odczyt >> t[0];
- for( int i=1; i<n; i++ )
- {
- odczyt >> t[i];
- if( t[i]>t[i-1] )
- mal = false;
- if( t[i]<t[i-1] )
- ros = false;
- }
- if( mal==ros )
- zapis << "NIE " << p_lin( t, n, x );
- else
- zapis << "TAK " << p_bin( t, 0, n, x, ros, mal );
- delete [] t;
- odczyt.close();
- zapis.close();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement