Cod sursa(job #544968)
Utilizator | Data | 2 martie 2011 15:27:11 | |
---|---|---|---|
Problema | Cautare binara | Scor | 0 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.4 kb |
#include<fstream.h>
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int a[100001],x,poz,p,u,w,n,m;
int main(){
f>>a[n]>>x;
poz=1;
p=1;
u=n;
w=1;
while(p<=u && w==1){
m=(p+u)/2;
if(a[m]=x){poz=m;w=0;} else{if(a[m]>=x){u=m-1;} else{p=m+1;}
}
if(poz!=-1)
cout<<x<<"se gaseste pe pozitia"<<poz<<'\n';
else cout<<"nu se gaseste";
}
g<<x<<poz;
g.close(); return 0;
}