Pagini recente » Cod sursa (job #1282640) | Cod sursa (job #571386) | Cod sursa (job #2225695) | Cod sursa (job #2657415) | Cod sursa (job #1517267)
#include<fstream>
using namespace std;
int a[100001],n,caz,s,nrx,poz,i,x;
int x0(int x)
{
int poz=-1,st=1 , dr=n ;
int m;
while (st<=dr)
{
m=(st+dr)/2;
if (x==a[m])
{
poz=m;
st=m+1;
}
else
if(x<a[m])
dr=m-1;
else
st=m+1;
}
return poz;
}
int x1(int x)
{
int poz=-1, st=1 , dr=n ,m;
while (st<=dr)
{
m=(st+dr)/2;
if (x>=a[m])
{
poz=m;
st=m+1;
}
else
dr=m-1;
}
return poz;
}
int x2(int x)
{
int poz=-1, st=1 , dr=n ,m;
while (st<=dr)
{
m=(st+dr)/2;
if (x<=a[m])
{
poz=m;
dr=m-1;
}
else
st=m+1;
}
return poz;
}
int main()
{
ifstream f("cautbin.in");
ofstream g("cautbin.out");
f>>n;
for(i=1; i<=n; i++)
f>>a[i];
f>>nrx;
for(i=1; i<=nrx; i++)
{
f>>caz;
f>>x;
if(caz==0)
g<<x0(x)<<endl;
if(caz==1)
g<<x1(x)<<endl;
if(caz==2)
g<<x2(x)<<endl;
}
f.close();
g.close();
return 0;
}