Pagini recente » Cod sursa (job #1283453) | Cod sursa (job #1367672) | Cod sursa (job #2371108) | Cod sursa (job #2489700) | Cod sursa (job #1025356)
#include <stdio.h>
int a[100],n;
void citire()
{
freopen ("cautbin.in","r",stdin);
scanf ("%d",&n);
for (int i=0;i<n;i++)
scanf ("%d ",&a[i]);
}
int cautare0(int st, int dr, int x)
{
while (st <= dr)
{
int mij = (st + dr)/2;
if (a[mij] == x)
{
while (a[mij]==x)
mij=mij+1;
return mij-1;
}
if (a[mij] < x)
st=mij+1;
else
dr=mij-1;
}
return -1;
}
int cautare1(int st, int dr, int x)
{
while (st <= dr)
{
int mij = (st + dr)/2;
if (a[mij] == x)
{
while (a[mij]==x)
mij=mij+1;
return mij-1;
}
if (a[mij] < x)
st=mij+1;
else
dr=mij-1;
}
return st-1;
}
int cautare2(int st, int dr, int x)
{
while (st <= dr)
{
int mij = (st + dr)/2;
if (a[mij] == x)
{
while (a[mij]==x)
mij=mij-1;
return mij+1;
}
if (a[mij] < x)
st=mij+1;
else
dr=mij-1;
}
return st;
}
int main()
{
freopen ("cautbin.out","w",stdout);
citire();
int nri;
scanf("%d",&nri);
for (int i = 0 ; i < nri ; ++i)
{
int in,x;
scanf("%d %d\n",&in,&x);
if (in == 0)
printf("%d\n",cautare0(0,n-1,x));
if (in == 1)
printf("%d\n",cautare1(0,n-1,x));
if (in == 2)
printf("%d\n",cautare2(0,n-1,x));
}
return 0;
}