Создать программу построения магического квадрата — Pascal(Паскаль)

при n=3
8 1 6
3 5 7
4 9 2

при n=10
51 0 62 0 73 0 84 0 95 0
0 61 0 72 0 83 0 94 0 60
70 0 71 0 82 0 93 0 59 0
0 80 0 81 0 92 0 58 0 69
79 0 90 0 91 0 57 0 68 0
0 89 0 100 0 56 0 67 0 78
88 0 99 0 55 0 66 0 77 0
0 98 0 54 0 65 0 76 0 87
97 0 53 0 64 0 75 0 86 0
0 52 0 63 0 74 0 85 0 96
const
  maxN = 10;
var
  a: array [0 .. maxN - 1, 0 .. maxN - 1] of Integer;
  n, hn, i, j, x, y: Integer;
  f: Text;
begin
  Assign(f, 'CON');
  Reset(f);
  ReadLn(f, n);
  Close(f);
  Assign(f, 'CON');
  Rewrite(f);
  if n = 2 then
  begin
    Write(f, 'No solution');
    Close(f);
    Halt(0);
  end;
  hn := n div 2;
  for i := 0 to n - 1 do
    for j := 0 to n - 1 do
    begin
      x := i + j - hn;
      y := i - j + hn;
      if x < 0 then
        inc(x, n)
      else if x >= n then
        dec(x, n);
      if y < 0 then
        inc(y, n)
      else if y >= n then
        dec(y, n);
      a[y][x] := i * n + j + 1;
    end;
  for y := 0 to n - 1 do
  begin
    for x := 0 to n - 1 do
      Write(f, a[y][x], ' ');
    WriteLn(f);
  end;
  Close(f);
end.

Leave a Comment

30 − 29 =