Pagini recente » Cod sursa (job #1787021) | Cod sursa (job #1376130) | Cod sursa (job #1085055) | Cod sursa (job #1753190) | Cod sursa (job #1517276)
#include<stdio.h>
#include <iostream>
#include<fstream>
using namespace std;
int a[100001],n,caz,nrx,poz,i,x;
int x0(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
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()
{
FILE *f = fopen("cautbin.in","r");
FILE *g = fopen("cautbin.out","w");
fscanf(f,"%d",&n);
for(i=1; i<=n; i++)
fscanf(f,"%d",&a[i]);
fscanf(f,"%d",&nrx);
for(i=1; i<=nrx; i++)
{
fscanf(f,"%d%d",&caz,&x);
if(caz==0)
fprintf(g,"%d\n",x0(x));
if(caz==1)
fprintf(g,"%d\n",x1(x));
if(caz==2)
fprintf(g,"%d\n",x2(x));
}
return 0;
}