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]<p then i2:=i2-1; 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
const n = 5; var a: array [1..n] of integer; i,k,x,l: integer; t: boolean; begin for i := 1 to n do read(a[i]); k := 0; for i := 1 to n do begin t := true; x := a[i]; if x < 0 then x := -x; l := 1; while l <= x do l := l*2; l := l div 2; while t and (l > 1) do begin t := x >= l; if t then x := x - l; t := t = (x mod 2 <> 0); x := x div 2; l := l div 4; end; if t then k := k + 1 end; write(k) end.
{2}
const n = 10; var a,d: array [1..n] of integer; i,j,k: integer; begin for i := 1 to n do a[i] := 100 + random(900); for i := 1 to n do begin d[i] := 0; for j := 1 to a[i] do if a[i] mod j = 0 then d[i] := d[i] + 1; end; for i := 1 to n-1 do begin k := i; for j := i + 1 to n do if d[j] < d[k] then k := j; j := a[k]; a[k] := a[i]; a[i] := j; j := d[k]; d[k] := d[i]; d[i] := j; end; for i := 1 to n do write(a[i],' ') end.
{3}
const n = 10; l = 3; var a: array [1..n] of integer; m: array [1..l] of integer; i,j,k: integer; begin for i := 1 to n do begin a[i] := random(1001); write(a[i],' ') end; writeln; for i := 1 to l do m[i] := -1; for i := 1 to n do begin j := 1; while j <= l do begin if (a[i] = m[j]) then j := l else if (a[i] < m[j]) or (m[j] = -1) then begin for k := l downto j+1 do m[k] := m[k-1]; m[j] := a[i]; j := l end; j := j + 1 end; end; for i := 1 to l do if m[i] <> -1 then write(m[i],' ') end.
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]<p then i2:=i2-1;
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
const
n = 5;
var
a: array [1..n] of integer;
i,k,x,l: integer;
t: boolean;
begin
for i := 1 to n do read(a[i]);
k := 0;
for i := 1 to n do
begin
t := true;
x := a[i]; if x < 0 then x := -x;
l := 1;
while l <= x do l := l*2; l := l div 2;
while t and (l > 1) do
begin
t := x >= l;
if t then x := x - l;
t := t = (x mod 2 <> 0);
x := x div 2;
l := l div 4;
end;
if t then k := k + 1
end;
write(k)
end.
{2}
const
n = 10;
var
a,d: array [1..n] of integer;
i,j,k: integer;
begin
for i := 1 to n do
a[i] := 100 + random(900);
for i := 1 to n do
begin
d[i] := 0;
for j := 1 to a[i] do
if a[i] mod j = 0 then
d[i] := d[i] + 1;
end;
for i := 1 to n-1 do
begin
k := i;
for j := i + 1 to n do
if d[j] < d[k] then k := j;
j := a[k];
a[k] := a[i];
a[i] := j;
j := d[k];
d[k] := d[i];
d[i] := j;
end;
for i := 1 to n do
write(a[i],' ')
end.
{3}
const
n = 10;
l = 3;
var
a: array [1..n] of integer;
m: array [1..l] of integer;
i,j,k: integer;
begin
for i := 1 to n do
begin
a[i] := random(1001);
write(a[i],' ')
end;
writeln;
for i := 1 to l do
m[i] := -1;
for i := 1 to n do
begin
j := 1;
while j <= l do
begin
if (a[i] = m[j]) then j := l else
if (a[i] < m[j]) or (m[j] = -1) then
begin
for k := l downto j+1 do
m[k] := m[k-1];
m[j] := a[i];
j := l
end;
j := j + 1
end;
end;
for i := 1 to l do
if m[i] <> -1 then write(m[i],' ')
end.