Pagini recente » Cod sursa (job #1976010) | Cod sursa (job #2663608) | Cod sursa (job #470660) | Cod sursa (job #260314) | Cod sursa (job #795806)
Cod sursa(job #795806)
#include<stdio.h>
int n,v[100001];
int caut0(int x){
int i=0,j=n-1,m;
while (i<j){
m=(j+i+1)/2;
if(x>=v[m])
i=m;
else
j=m-1;
}
if(v[i]==x)
return i;
return -1;
}
int caut1(int x){
int i=0,j=n-1,m;
while(i<j){
m=(j+i+1)/2;
if(x>=v[m])
i=m;
else
j=m-1;
}
return i;
}
int caut2(int x){
int i=0,j=n-1,m;
while(i<j){
m=(j+i)/2;
if(x<=v[m])
j=m;
else
i=m+1;
}
return i;
}
int main(){
FILE *fin,*fout;
fin=fopen("cautbin.in","r");
fout=fopen("cautbin.out","w");
int i,m,x,y;
fscanf(fin,"%d",&n);
for(i=0;i<n;i++)
fscanf(fin,"%d",&v[i]);
fscanf(fin,"%d",&m);
for(i=0;i<m;i++){
fscanf(fin,"%d%d",&x,&y);
if(x==0){
m=caut0(y);
fprintf(fout,"%d\n",m+1);
}
if(x==1){
m=caut1(y);
fprintf(fout,"%d\n",m+1);
}
if(x==2){
m=caut2(y);
fprintf(fout,"%d\n",m+1);
}
}
fclose(fin);
fclose(fout);
return 0;
}