【问题描述】方阵填数:在一个NXN的方阵中,填入1,2, NXN个数,并要求
构成如下的格式:
如 N:=4
10 11 12 1
9 16 13 2
8 15 14 3
7 6 5 4
N =5
13 14 15 16 1
12 23 24 17 2
11 22 25 18 3
10 21 20 19 4
9 8 7 6 5
program yzkx2016_6;
var
j:Integer;
n,i,k,x,y: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.