題目描述
矩陣中填數(shù),當給N*N的矩陣,要求用程序填入下列形式的數(shù):
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
輸出格式
輸出符合以上規(guī)律的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.