Pagini recente » Cod sursa (job #1965052) | Cod sursa (job #235766) | Cod sursa (job #3237176) | Cod sursa (job #2350673) | Cod sursa (job #2091440)
#include <iostream>
#include <fstream>
using namespace std;
int n, m, x[100005], poz,tip,r;
int caut0(int a)
{
int st=1, dr=n, mij;
while (st<=dr)
{
mij=st+(dr-st)/2;
if (x[mij]>=a)
{
if (a==x[mij])
poz=mij;
st=mij+1;
}
else if (x[mij]<a)
{
dr=mij-1;
}
}
return poz;
}
int caut1(int a)
{
int st=1, dr=n, mij;
while (st<=dr)
{
mij=st+(dr-st)/2;
if (x[mij]>a)
{
dr=mij-1;
}
else if (x[mij]<=a)
{
poz=mij;
st=mij+1;
}
}
return poz;
}
int caut2(int a)
{
int st=1, dr=n, mij;
while (st<=dr)
{
mij=st+(dr-st)/2;
if (x[mij]<a)
st=mij+1;
else if (x[mij]>=a)
{
poz=mij;
dr=mij-1;
}
}
return poz;
}
int main()
{
ifstream f("cautbin.in");
ofstream g("cautbin.out");
f >> n;
for (int i=1; i<=n; i++)
{
f >> x[i];
}
f >> m;
for (int j=0; j<m; j++)
{
f >> tip >>r;
if (tip==0)
g << caut0(r) << '\n';
else if (tip==1)
g << caut1(r) <<'\n';
else if (tip==2)
g << caut2(r) <<'\n';
}
return 0;
}