pascal 奇数阶幻方

来源:百度知道 编辑:UC知道 时间:2024/06/25 05:43:30
用pascal实现生成任意奇数阶幻方

var
x,y,z,m,n,i,j:integer;
a:array[1..1000,1..1000] of integer;
begin
readln(m);
if m mod 2=1 then
begin
n:=(m div 2)+1;
for i:=1 to m do
begin
for j:=1 to m do
a[i,j]:=0;
end;
i:=1+1;j:=n+1;y:=1;
for z:=1 to m*m do
begin
i:=i-1;j:=j-1;
if i=0 then
begin
i:=m;
end;
if j=0 then
begin
j:=m;
end;
if a[i,j]<>0 then
begin
i:=i+2;
j:=j+1;
if i>m then
i:=i-m;
if j>m then
j:=j-m;
end;
a[i,j]:=y;
y:=y+1;
end;