Cod sursa(job #2630468)
Utilizator | Data | 26 iunie 2020 09:39:40 | |
---|---|---|---|
Problema | Cautare binara | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 1.83 kb |
#include<iostream>
#include<fstream>
using namespace std;
int n,x,cerinta,v[100001],m,st=1,dr,pos=-1;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int main(){
f>>n;
for(int i=1;i<=n;i++)
f>>v[i];
f>>m;
for(int i=1;i<=m;i++)
{
f>>cerinta;
if(cerinta==0)
{
f>>x;
st=1;
dr=n;
pos=-1;
while(st<=dr)
{
int mij=(st+dr)/2;
if(v[mij]<x)
{
st=mij+1;
}
if(v[mij]>x)
{
dr=mij-1;
}
if(v[mij]==x)
{
pos=mij;
st=mij+1;
}
}
g<<pos<<"\n";
}
if(cerinta==1)
{
f>>x;
st=1;
dr=n;
pos=-1;
while(st<=dr)
{
int mij=(st+dr)/2;
if(v[mij]<x)
{
pos=mij;
st=mij+1;
}
if(v[mij]>x)
{
dr=mij-1;
}
if(v[mij]==x)
{
pos=mij;
st=mij+1;
}
}
g<<pos<<"\n";
}
if(cerinta==2)
{
f>>x;
st=1;
dr=n;
pos=-1;
while(st<=dr)
{
int mij=(st+dr)/2;
if(v[mij]<x)
{
st=mij+1;
}
if(v[mij]>x)
{
pos=mij;
dr=mij-1;
}
if(v[mij]==x)
{
pos=mij;
dr=mij-1;
}
}
g<<pos<<"\n";
}
}
}