主题:“螺旋方阵”问题,向大家请教,希望能给出程序说明,谢谢
96131
[专家分:0] 发布于 2006-04-28 09:04:00
2. “螺旋方阵”
下图1—3所示,分别为n=2、3、4、时的数字右螺旋方阵。输入n(1<=n<=9) 输出n的方阵。
1 2 1 2 3 1 2 3 4
4 3 8 9 4 12 13 14 5
7 6 5 11 16 15 6
10 9 8 7
回复列表 (共3个回复)
沙发
贺天行宝 [专家分:2300] 发布于 2006-04-28 20:13:00
var
s:array[1..100,1..100]of integer;
i,j,k,l,m,n:integer;
begin
readln(n);m:=1;i:=1;j:=1;
if n=1 then begin writeln(' 1'); exit; end;
for k:=1 to n-1 do
begin
for l:=1 to n-k+1-(k-1) do begin if s[i,j]<>0 then break; s[i,j]:=m; inc(j); inc(m); end; inc(i); dec(j);
for l:=1 to n-k-1-(k-1) do begin if s[i,j]<>0 then break; s[i,j]:=m; inc(i); inc(m); end;
for l:=1 to n-k+1-(k-1) do begin if s[i,j]<>0 then break; s[i,j]:=m; dec(j); inc(m); end; inc(j); dec(i);
for l:=1 to n-k-1-(k-1) do begin if s[i,j]<>0 then break; s[i,j]:=m; dec(i); inc(m); end; inc(i); inc(j);
if s[i,j]<>0 then break;
end;
for i:=1 to n do
begin
for j:=1 to n do
write(s[i,j]:5);
writeln;
end;
end.
板凳
96131 [专家分:0] 发布于 2006-04-29 10:41:00
太谢谢了
3 楼
edwin89102 [专家分:50] 发布于 2006-05-04 19:47:00
Var
j:Integer;
n,i,k,x,y,Number:Byte;
Result:Array[1..25,1..25] of Byte;
Begin
Write('N='); Readln(n);
For x:=1 to n do for y:=1 to n do Result[x,y]:=0; {初始化数组}
X:=0; Y:=N; i:=0; k:=N; j:=1;
{j表示填数方向。从左→右或从上→下,j=1;否则j=-1}
While i<N*N do
Begin
For i:=i+1 to i+k do {纵向填 k 个数}
Begin
X:=X+j;
Result[x,y]:=i;
End;
Dec(k); {K:填多少个数,其规律是递减:n,n-1,n-2,......,2,1}
If j=1 then j:=-1 else j:=1; {改变方向}
For i:=i+1 to i+k do {横向填 k 个数}
Begin
y:=y+j;
Result[x,y]:=i;
End;
End;
For x:=1 to n do {输出}
Begin
For y:=1 to n do Write(Result[x,y]:3);
Writeln;
End;
End.
我来回复