使用 Delphi 7 在控制台上进行特殊数组排序

Special array sorting on console with Delphi 7

我的整数数组是:

AR: array [0..5] of integer = (6, 5, 4, 8, 9, 7);

算法:

Take the first value of the AR array (6)
Find the first great value that comes after it (9)
Find out how ahead you are after yourself (4)
write this as the first value in the result array
....
Get the second value in the AR index (5)
The first great value after him (9)
Find out how ahead you are after yourself (3)
write 3 after 4 in our results series.
.
.

将对每个数组条目重复此过程。如果没有大于后面的值,就会写成'0'。最后一个值必须是 '0'.

预期输出必须是:

OutValue = 4-3-2-1-0-0

我没有真正有效的代码。

const
AR:array [0..5] of integer = (6, 5, 4, 8, 9, 7)
var
S:Tstringlist;
output:string;
i,j,g,n:integer;
begin
S:=Tstringlist.create;
for i:=0 to 5 do
for j:=i+1 to 5 do begin
if AR[i] > AR[j] then begin
g:=0;
AR[i]:=AR[j];
AR[i]:=g;
S.add( inttostr(g));
end
else if AR[i] < AR[j] then
begin
g:=sizeof(maxintvalue(AR[i]));
AR[i]:=AR[j];
AR[i]:=g;
S.add( inttostr(g));
end;
end;
For i:=0 to 5 do
begin
//S.Sorted:=true;
output:=S.Text;
end;
S.free;
writeln(output);
readln;
end.

谢谢。

希望我理解正确。以下方法(使用嵌套 for 循环)是您问题的可能解决方案。

program SpecialArraySort;
{$APPTYPE CONSOLE}

uses
  SysUtils;

procedure SortArray;
const
   aInput: array [0..5] of integer = (6, 5, 4, 8, 9, 7);
var
   aOutput: array [0..5] of integer;
   i, j, value, offset: Integer;
begin
   // Generate output
   for i := 0 to 5 do begin
      value  := aInput[i];
      offset := 0;
      for j := i + 1 to 5 do begin
         if value < aInput[j] then begin
            value  := aInput[j];
            offset := j - i;
         end{if};
      end;
      aOutput[i] := offset;
   end;
   // Print output
   for i := 0 to 5 do begin
      Write(IntToStr(aOutput[i]) + ' ');
   end;
end;

begin
   SortArray;
end.