主题:幻方一解
program whb(input,output);
var k,i,j,n:integer;
a:array[0..100,0..100]of integer;
f:array[0..100,0..100]of boolean;
begin
write('please input an odd number:');
readln(n);
for i:=0 to n do
for j:=0 to n do
f[i,j]:=false;
f[0,0]:=true;
i:=1;j:=n div 2+1;
a[i,j]:=1;f[i,j]:=true;
for k:=2 to sqr(n) do
begin
if (i=1) and (j<>1) then
begin
i:=n;
j:=j-1;
end
else if (j=1) and (i<>1) then
begin
i:=i-1;
j:=n;
end
else
begin
i:=i-1;
j:=j-1;
if f[i,j]=true then begin
i:=i+2;
j:=j+1;
end
end;
a[i,j]:=k;
f[i,j]:=true;
end;
for i:=1 to n do
begin
for j:=1 to n do
write(a[i,j]:3);
writeln;
end;
end.
说明:此题用的是楼梯法。。具体说明见幻方构造。
var k,i,j,n:integer;
a:array[0..100,0..100]of integer;
f:array[0..100,0..100]of boolean;
begin
write('please input an odd number:');
readln(n);
for i:=0 to n do
for j:=0 to n do
f[i,j]:=false;
f[0,0]:=true;
i:=1;j:=n div 2+1;
a[i,j]:=1;f[i,j]:=true;
for k:=2 to sqr(n) do
begin
if (i=1) and (j<>1) then
begin
i:=n;
j:=j-1;
end
else if (j=1) and (i<>1) then
begin
i:=i-1;
j:=n;
end
else
begin
i:=i-1;
j:=j-1;
if f[i,j]=true then begin
i:=i+2;
j:=j+1;
end
end;
a[i,j]:=k;
f[i,j]:=true;
end;
for i:=1 to n do
begin
for j:=1 to n do
write(a[i,j]:3);
writeln;
end;
end.
说明:此题用的是楼梯法。。具体说明见幻方构造。