主题:第十一届奥赛(初赛)程序阅读第2题(求讲解过程)
2. var a : array [1..50] of integer;
n, i, sum : integer;
2. Var a : array [1..50] of integer;
n, i, sum : integer;
procedure work(p, r: integer);
var i, j, temp : integer;
begin
if p < r then begin
i := p - 1;
for j := p to r - 1 do
if a[j] >= a[r] then begin
inc(i);
temp := a[i]; a[i] := a[j]; a[j] := temp;
end;
temp := a[i + 1]; a[i + 1] := a[r]; a[r] := temp;
work(p, i);
work(i + 2, r);
end;
end;
begin
read(n);
for i := 1 to n do read(a[i]);
work(1, n);
for i := 1 to n - 1 do sum := sum + abs(a[i + 1] - a[i]);
writeln(sum);
end.
输入:10 23 435 12 345 3123 43 456 12 32 -100
输出:
我是个初学者,希望得到大家的帮助!谢谢
n, i, sum : integer;
2. Var a : array [1..50] of integer;
n, i, sum : integer;
procedure work(p, r: integer);
var i, j, temp : integer;
begin
if p < r then begin
i := p - 1;
for j := p to r - 1 do
if a[j] >= a[r] then begin
inc(i);
temp := a[i]; a[i] := a[j]; a[j] := temp;
end;
temp := a[i + 1]; a[i + 1] := a[r]; a[r] := temp;
work(p, i);
work(i + 2, r);
end;
end;
begin
read(n);
for i := 1 to n do read(a[i]);
work(1, n);
for i := 1 to n - 1 do sum := sum + abs(a[i + 1] - a[i]);
writeln(sum);
end.
输入:10 23 435 12 345 3123 43 456 12 32 -100
输出:
我是个初学者,希望得到大家的帮助!谢谢