求Fortran大神说一下n×n维逆时针方阵怎么做 10
2016-06-28
展开全部
以下是pascal程序,供参考。不管什么语言,思路是基本相同的。
var
a,b,t,x:longint; {a行号,b为列号,t为顺序号,x为矩阵阶数}
s:array[0..100,0..100]of longint;
begin
x:=10;
for a:=1 to x+1 do begin
s[0,a]:=1; s[x+1,a]:=1; s[a,0]:=1; s[a,x+1]:=1;
{设定方阵的外边界值为1}
end;
t:=1; a:=1; b:=1;
while t<=x*x do begin
s[a,b]:=t;
if (s[a+1,b]<>0)and(s[a-1,b]=0)and(s[a,b+1]<>0)then a:=a-1
{当在右侧行进时,行号-1,即向上行}
else
if (s[a,b+1]=0)and(s[a+1,b]<>0)and(s[a,b-1]<>0)then b:=b+1
{当在下侧行进时,列号+1,即向右行}
else
if (s[a+1,b]=0)and(s[a-1,b]<>0)and(s[a,b-1]<>0)then a:=a+1
{当行左侧行进时,行号+1,即向下行}
else
b:=b-1; {当在上部行进时,列号-1,即向左行}
t:=t+1;
end;
for a:=1 to x do begin
for b:=1 to x do write(s[a,b]:5,' ');
writeln;
end;
end.
var
a,b,t,x:longint; {a行号,b为列号,t为顺序号,x为矩阵阶数}
s:array[0..100,0..100]of longint;
begin
x:=10;
for a:=1 to x+1 do begin
s[0,a]:=1; s[x+1,a]:=1; s[a,0]:=1; s[a,x+1]:=1;
{设定方阵的外边界值为1}
end;
t:=1; a:=1; b:=1;
while t<=x*x do begin
s[a,b]:=t;
if (s[a+1,b]<>0)and(s[a-1,b]=0)and(s[a,b+1]<>0)then a:=a-1
{当在右侧行进时,行号-1,即向上行}
else
if (s[a,b+1]=0)and(s[a+1,b]<>0)and(s[a,b-1]<>0)then b:=b+1
{当在下侧行进时,列号+1,即向右行}
else
if (s[a+1,b]=0)and(s[a-1,b]<>0)and(s[a,b-1]<>0)then a:=a+1
{当行左侧行进时,行号+1,即向下行}
else
b:=b-1; {当在上部行进时,列号-1,即向左行}
t:=t+1;
end;
for a:=1 to x do begin
for b:=1 to x do write(s[a,b]:5,' ');
writeln;
end;
end.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询