Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Центр помощи > [Turbo Pascal]


Автор: baldman88 5.12.2009, 20:59
Задача:
Создать программу заполняющую матрицу 9х9  по спирали от центра: вниз-влево-вверх-вправо.
Вот что придумал:
Код

program lab4;
var
    arr: array[1..9, 1..9] of integer;
    i, x, y, last_x, last_y, count: integer;
begin
    i:= 0;
    x:= 5;
    y:= 5;
    count:= 1;
    last_x:= x;
    last_y:= y;
    arr[x, y]:= count;
    while (i < 9) do begin
        for x:= (last_x + 1) to (last_x + (i + 1)) do begin
            if count = 81 then break;
            count:= count + 1;
            arr[x, y]:= count;
        end;
        if count = 81 then break;
        last_x:= last_x + (i + 1);
        x:= last_x;
        for y:= (last_y - 1) downto (last_y - (i + 1)) do begin
            count:= count + 1;
            arr[x, y]:= count;
        end;
        last_y:= last_y - (i + 1);
        y:= last_y;
        for x:= (last_x - 1) downto (last_x - (i + 2)) do begin
            count:= count + 1;
            arr[x, y]:= count;
        end;
        last_x:= last_x - (i + 2);
        x:= last_x;
        for y:= (last_y + 1) to (last_y + (i + 2)) do begin
            count:= count + 1;
            arr[x, y]:= count;
        end;
        last_y:= last_y + (i + 2);
        y:= last_y;
        i:= i + 2;
    end;
    for x:= 1 to 9 do begin
        for y:= 1 to 9 do
            write(arr[x, y]: 3);
        writeln;
    end;
    writeln;
    write('Press any key to continue...');
    readln;
end.

Может можно сделать более элегантно?

Автор: ТоляМБА 5.12.2009, 21:33
http://forum.pascal.net.ru/index.php?showtopic=2694&st=0&p=27640&#entry27640

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)