打印下列图形成 用PASCAL做

7、 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9
2024-12-19 09:39:02
推荐回答(2个)
回答1:

这是螺旋方阵啊,只有5阶,这里有个可以n阶的程序,没有过多解释,不过是方向数组,希望楼主能看懂:
const
dx:array[0..3] of longint=(0,1,0,-1);
dy:array[0..3] of longint=(1,0,-1,0);
var
a:array[0..21,0..21] of longint;
n,x,y,i,j,k,t:longint;
begin
readln(n);
for i:=0 to n+1 do
for j:=0 to n+1 do
a[i,j]:=1;
for i:=1 to n do
for j:=1 to n do
a[i,j]:=0;
x:=1;y:=1;
k:=0;
for t:=1 to n*n do
begin
a[x,y]:=t;
if a[x+dx[k],y+dy[k]]>0 then k:=(k+1) mod 4;
x:=dx[k]+x;
y:=dy[k]+y;
end;
for i:=1 to n do
begin
for j:=1 to n do
write(a[i,j]:5);
writeln;
end;
end.

回答2:

Var
r :array[1..100,1..100] of integer;
i, j, k, n, s : integer;
begin
readln(n);
j:=1; i:=0; s:=0; k:=n;
repeat
k:=k-1;
while i+j begin s:=s+1; i:=i+1; r[i,j]:=s end;
while i+j<2*(k+1) do
begin s:=s+1; j:=j+1; r[i,j]:=s end;
while i+j>n+1 do
begin s:=s+1; i:=i-1; r[i,j]:=s end;
while i+j>2*(n-k)+1 do
begin s:=s+1; j:=j-1; r[i,j]:=s end;
until s>=n*n;
for i:=1 to n do
begin
for j:=1 to n do write(r[i,j]:4);
writeln
end;
readln
end.
可以借助n×n矩阵的n和自定控制变量k,来有效地控制数据的拐点,即控制程序中各个小循环体的进出。
给点分吧!