pascal 奇数阶幻方
来源:学生作业帮 编辑:神马作文网作业帮 分类:综合作业 时间:2024/11/12 02:35:22
pascal 奇数阶幻方
用pascal实现生成任意奇数阶幻方
用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;
end;
for i:=1 to m do
begin
for j:=1 to m do
write(a[i,j]:5);
writeln;
end;
readln;
end.
我试过了,可以的.(但只限于边长为奇数)
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;
end;
for i:=1 to m do
begin
for j:=1 to m do
write(a[i,j]:5);
writeln;
end;
readln;
end.
我试过了,可以的.(但只限于边长为奇数)