Специальная сортировка массивов на консоли с Delphi 7 - PullRequest
0 голосов
/ 15 апреля 2020

Мой массив целых чисел:

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.

Спасибо.

1 Ответ

2 голосов
/ 16 апреля 2020

Надеюсь, я правильно понял вопрос. Следующий подход (с использованием вложенного for l oop) является возможным решением вашей проблемы.

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.
...