Var a:array of integer;
n,i,p,i1,i2: integer;
begin
write('n = ');
readln(n);
SetLength(a,n);
Randomize;
a[0]:=random(50)+100;
write(a[0]:4);
for i:=1 to n-1 do
begin
a[i]:=a[i-1]-random(11);
write(a[i]:4);
end;
writeln;
p:=a[0]-a[n-1];
writeln('p = ',p);
i1:=0; i2:=n-1;
repeat
i:=(i1+i2) div 2;
if a[i]>p then i1:=i1+1;
if a[i]
until (a[i]=p)or(i1>=i2);
if a[i]=p
then writeln('i = ',i)
else writeln('Искомого элемента нет');
end.
Пример:
n = 20
142 139 135 129 120 120 120 113 107 106 104 99 89 88 81 72 71 70 60 54
p = 88
i = 13