Дано действительное число x и массив A[n]. В массиве найти два члена, среднее арифметическое которых ближе всего к x — Pascal(Паскаль)

var
a:array[1..100]of integer;
min,x:real;
i,j,n,c1,c2,nomc1,nomc2:integer;
begin
randomize;
writeln('vvedite kol-vo elementov');
readln(n);
writeln('massiv');
for i:=1 to n do
begin
a[i]:=random(20);
write(a[i],' ');
end;
writeln;
writeln('vvedite chislo x');
readln(x);
min:=1E+38;
for i:=1 to n do
for j:=1 to n do
if i<>j then
 if abs((a[i]+a[j])/2-x)<min then
begin
min:=abs((a[i]+a[j])/2-x);
c1:=a[i];
nomc1:=i;
c2:=a[j];
nomc2:=j;
end;
writeln ('a[',nomc1,']=',c1);
writeln ('a[',nomc2,']=',c2);
readln;
end.

Leave a Comment

− 1 = 1