В квадратной матрице переставить строки - Pascal

Узнай цену своей работы

Формулировка задачи:

В квадратной матрице переставить строки так, чтобы количество ненулевых элементов в строках росла от первой строки до последней.

Решение задачи: «В квадратной матрице переставить строки»

textual
Листинг программы
type
    TLine = Array[Byte] Of ShortInt;
    TArray = Array[Byte] Of TLine;
var
    i, j, n, tempCount: Byte;
    ar: TArray;
    tempLine: TLine;
    counts: Array[Byte] Of Byte;
begin
    repeat
        Write('Input n: ');
        ReadLn(n);
    until (n > 0);
    Randomize();
    for i := 0 to n - 1 do
    begin
        counts[i] := 0;
        for j := 0 to n - 1 do
        begin
            ar[i, j] := Random(11) - 5;
            Write(ar[i, j]:3);
            if (ar[i, j] <> 0)
            then
                Inc(counts[i]);
        end;
        WriteLn;
    end;
    WriteLn; WriteLn;
    for i := n - 2 downto 0 do
        for j := 0 to i do
            if (counts[j] > counts[j + 1])
            then
            begin
                tempCount := counts[j];
                tempLine := ar[j];
                counts[j] := counts[j + 1];
                ar[j] := ar[j + 1];
                counts[j + 1] := tempCount;
                ar[j + 1] := tempLine;
            end;
    for i := 0 to n - 1 do
    begin
        for j := 0 to n - 1 do
            Write(ar[i, j]:3);
        WriteLn;
    end;
end.

Оцени полезность:

14   голосов , оценка 4.071 из 5
Похожие ответы