【问题描述】方阵填数:在一个N×N的方阵中,填入1,2,.....N×N个数,并要构成如下的格式:
如 N=4 N=5
10 11 12 1 13 14 15 16 1
9 16 13 2 12 23 24 17 2
8 15 14 3 11 22 25 18 3
7 6 5 4 10 21 20 19 4
9 8 7 6 5
【参考程序】
Var
j:Integer;
n,i,k,x,y,Number:Byte;
Result:Array[1..25,1..25] of Byte;
Begin
Write('N='); Readln(n);
For x:=1 to n do for y:=1 to n do Result[x,y]:=0;
X:=0; Y:=N; i:=0; k:=N; j:=1;
While i< n*n do
Begin
For i:=i+1 to i+k do
Begin
x:=x+j ;
Result[x,y]:=i;
End;
Dec(k);
If j=1 then j:=-1 else j:=1;
For i:=i+1 to i+k do
Begin
y:=y+j;
Result[x,y]:=i;
End;
End; For x=1 to n do
Begin
For y:=1 to n do Write(Result[x,y]:3);
Writeln;
End;
End.