Chương trình ví dụ:
Mã: Chọn tất cả
Moi ban nhap vao so N: 4
Ta duoc bang sau:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
Mã: Chọn tất cả
Uses Crt;
Var N, X, k : Integer;
M : Array[1..100, 1..100] Of Integer;
Left, Right, Top, Bottom : Integer;
Begin
ClrScr;
N := 0;
While ((N < 3) Or (N > 100)) Do
Begin
Write('Moi ban nhap vao so N: ');
ReadLn(N);
End;
WriteLn('Ta duoc bang sau:');
X := 1;
Left := 1;
Right := N;
Top := 1;
Bottom := N;
While (X <= N * N) Do
Begin
For k := Left To Right Do Begin M[k, Top] := X; Inc(X); End;
Inc(Top);
For k := Top To Bottom Do Begin M[Right, k] := X; Inc(X); End;
Dec(Right);
For k := Right DownTo Left Do Begin M[k, Bottom] := X; Inc(X); End;
Dec(Bottom);
For k := Bottom DownTo Top Do Begin M[Left, k] := X; Inc(X); End;
Inc(Left);
End;
For X := 1 To N Do
Begin
For k := 1 To N Do Write(M[k, X] : 4);
WriteLn;
End;
ReadLn;
End.