procedure TForm1.Button1Click(Sender: TObject);
begin
panel1.Caption:='';
repeat
n:=StrToInt(InputBox('Ввод данных','Ввод количества элементов',''));
until n>2;
for i:=0 to n-1 do
begin
StringGrid1.Cells[i,0]:='';
StringGrid2.Cells[i,0]:='';
end;
StringGrid1.Colcount:=n;
StringGrid1.Width:=n*StringGrid1.DefaultColWidth+15;
StringGrid1.Visible:=true;
StringGrid2.Colcount:=n;
StringGrid2.Width:=n*StringGrid2.DefaultColWidth+15;
StringGrid2.Visible:=true;
for i:=1 to n do
begin
repeat
st:=InputBox('ввод данныx','a('+IntToStr(i)+')=','');
val( st,a,code);
until code=0;
StringGrid1.Cells[i-1,0]:=intToStr(a);
end;
p:=0;
i:=0;
while (p=0) and (i<=n-1) do
begin
i:=i+1;
if a<0 then p:=1;
end;
s:=0;
j:=n+1;
while (s=0) and (j>=2) do
begin
j:=j-1;
if a[j]>0 then s:=1;
end;
if (p=1) and (s=1) then
begin
f:=a;
a:=a[j];
a[j]:=f;
end;
for i := 1 to n do
StringGrid2.Cells[i-1,0]:=intToStr(a);
end;