Pagini recente » Cod sursa (job #1340007) | Cod sursa (job #725042) | Cod sursa (job #758667) | Cod sursa (job #1556376) | Cod sursa (job #1301648)
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<algorithm>
#include<climits>
using namespace std;
int c0(int v[],int n,int x)
{
int st,dr,mij;
st=0;
dr=n-1;
int poz=-2;
while(st<dr)
{
mij=(st+dr)/2;
if(v[mij]==x)
{
poz=mij;
st=mij+1;
}
else
if(v[mij]>x)
dr=mij-1;
else
st=mij+1;
}
if(st==dr)
{
if(v[st]==x)
poz=st;
}
return poz;
}
//.................................
int c1(int v[],int n,int x)
{
int poz=-1,st=0,dr=n-1,mij;
while(st<dr)
{
mij=(st+dr)/2;
if(v[mij]==x)
{
poz=mij;
st=mij+1;
}
else
if(v[mij]>x)
dr=mij-1;
else
{
st=mij+1;
poz=mij;
}
}
if(st==dr)
{
if(v[st]<=x)
poz=st;
}
return poz;
}
int c2(int v[],int n,int x)
{
int poz=-1,st=0,dr=n-1,mij;
while(st<dr)
{
mij=(st+dr)/2;
if(v[mij]==x)
{
poz=mij;
dr=mij-1;
}
else
if(v[mij]>x)
{
dr=mij-1;
poz=mij;
}
else
st=mij+1;
}
if(st==dr)
{
if(v[st]>=x)
poz=st;
}
return poz;
}
int main()
{
ifstream si;
si.open("cautbin.in");
FILE* so=fopen("cautbin.out","w");
int n;
si>>n;
int i;
int v[n];
for(i=0;i<n;++i)
{
si>>v[i];
}
int m;
si>>m;
int a,b;
while(m--)
{
si>>a>>b;
if(a==0)
fprintf(so,"%i\n",c0(v,n,b)+1);
else
if(a==1)
fprintf(so,"%i\n",c1(v,n,b)+1);
else
fprintf(so,"%i\n",c2(v,n,b)+1);
}
}