题目描述
矩阵中填数,当给N*N的矩阵,要求用程序填入下列形式的数:
1 3 4 10 11
2 5 9 12 19
6 8 13 18 20
7 14 17 21 24
15 16 22 23 25
输入格式
输入N
输出格式
输出符合以上规律的n*n矩阵
代码
var
i,j,n,m,x,y,f,k,t:integer;
s:string;
var arr:array[1..10000,1..10000] of integer;
begin
readln(n);
m:=1;
for i:=1 to n do
begin
if((i mod 2)=0) then
begin
f:=-1;
x:=i;
y:=1;
end
else
begin
f:=1;
x:=1;
y:=i;
end;
for j:=0 to i-1 do
begin
arr[x+f*j,y-f*j]:=m;
arr[n-(x+f*j)+1,n-(y-f*j)+1]:=n*n-(m-1);
inc(m);
end;
end;
str(n*n,s);
k:=length(s);
for i:=1 to n do
begin
for j:=1 to n do
begin
write(arr[i,j],' ');
str(arr[i,j],s);
t:=length(s);
while(t<=k) do
begin
write(' ');
inc(t);
end;
end;
writeln('');
end;
readln;
end.