Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int n, a[100], x;
- int caut(int st, int dr)
- {
- int m;
- if(st==dr && a[st]==x)
- return st;
- if(st==dr && a[st]!=x)
- return 0;
- else
- {
- m=(st+dr)/2;
- if(x==a[m])
- return m;
- else if(x<a[m])
- caut(st, m-1);
- else
- caut(m+1, dr);
- }
- }
- int main()
- {
- int i;
- cin>>n;
- cin>>x;
- for(i=1; i<=n; i++)
- cin>>a[i];
- if(caut(1,n)==0)
- cout<<"Numarul cautat nu este pe nicio pozitie.";
- else
- cout<<"Numarul cautat este pe pozitia: "<<caut(1,n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement