Cod sursa(job #196658)

Utilizator theratmantheratman theratman Data 27 iunie 2008 20:00:07
Problema Cautare binara Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 0.76 kb
var f,g:text;
    a:array[1..100000] of int64;
    op,x,n,m:int64;
    i:longint;
function cautbin(st,dr:longint):longint;
 var poz,m:longint;
 begin
 poz:=0;
 while (st<dr)and(poz=0) do begin
  m:=(st+dr) div 2;
  if a[m]=x then poz:=m;
  if a[m]<x then st:=m+1
            else dr:=m-1;
 end;
 if op=0 then
  if poz=0 then cautbin:=-1
           else cautbin:=poz;
 if op=1 then
  if poz=0 then cautbin:=m
           else cautbin:=poz;
 if op=2 then
  if poz=0 then cautbin:=m+1
           else cautbin:=poz;
 end;
begin
assign(f,'cautbin.in');reset(f);
assign(g,'cautbin.out');rewrite(g);
read(f,n);
for i:=1 to n do read(f,a[i]);
read(f,m);
for i:=1 to m do begin
 read(f,op,x);
 writeln(g,cautbin(1,n));
end;
close(f);
close(g);
end.