Задача: Создать программу заполняющую матрицу 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.
|
Может можно сделать более элегантно? |