Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///pentru catarea bina la numere crescatoare
- #include<iostream>
- using namespace std;
- int v[1000];
- int main()
- {
- int st,dr,poz,n,m,x;
- ///n e numarul de numere
- ///x e numarul ce trebuie cautat
- cin>>n>>x;
- for(int i=1; i<=n; i++)
- {
- cin>>v[i];
- }
- ///citesc vectorul
- st=1;
- dr=n;
- poz=0;
- ///atribui valori la dreapta ,stanga,pozitie
- while(st < dr && poz == 0)
- {
- m=(st+dr)/2;
- if(v[m]==x)
- poz=m;
- else
- if (v[m]<x)
- {
- st=m+1;
- }
- else
- {
- dr=m-1;
- }
- }
- cout<<poz;
- }
Add Comment
Please, Sign In to add comment