主题:怎么用PASCAL打出顺时针螺旋图
stardcl
[专家分:0] 发布于 2008-09-20 17:40:00
[em2]怎么用PASCAL打出顺时针螺旋图?谢谢了我
回复列表 (共4个回复)
沙发
小田甜 [专家分:3910] 发布于 2008-09-21 12:22:00
螺旋渐开还是渐闭?
开一个足够大的数组,然后从中间while。
板凳
jack-jiao [专家分:140] 发布于 2008-10-02 15:35:00
aprogram lxsz;
const
fx:array[1..4,1..2] of -1..1=((0,1),(1,0),(0,-1),(-1,0));
var a:array[0..401,0..401]of integer;
x,y,i,j,m,c,n:integer;
begin
write('n=');
readln(n);
for i:=0 to 1+n do
for j:=0 to n+1 do
if (i=0) or(j=0)or(i=n+1)or(j=n+1)
then a[i,j]:=1 else a[i,j]:=0;
c:=1;
x:=1;y:=1;
a[x,y]:=c; inc(c);
m:=n*n;
j:=1;
while (c<=m) do
begin
x:=fx[j,1]+x; y:=fx[j,2]+y;
if a[x,y]<>0 then
begin
x:=x-fx[j,1];y:=y-fx[j,2];
inc(j);
if j=5 then j:=1;
end
else
begin
a[x,y]:=c;inc(c);
end;
end;
for i:=1 to n do
begin
for j:=1 to n do write(a[i,j]:4);
writeln;
end;
end.
只是一种解法而已!!
3 楼
pascal玩家 [专家分:280] 发布于 2008-10-18 15:23:00
program lxt;
var a:array[1..50,1..50] of integer;
i,j,n,t,o:integer;
ch:char;
b:array[1..50,1..50] of boolean;
label 10;
begin
10:read(n);
for i:=1 to n do
for j:=1 to n do
b[i,j]:=true;
i:=1;j:=0;
ch:='r';
repeat
t:=t+1;
case ch of
'r':begin j:=j+1; a[i,j]:=t; b[i,j]:=false;
if (j+1>n) or (b[i,j+1]=false) then ch:='d'; end;
'd':begin i:=i+1; a[i,j]:=t; b[i,j]:=false;
if (i+1>n) or (b[i+1,j]=false) then ch:='l'; end;
'l':begin j:=j-1; a[i,j]:=t; b[i,j]:=false;
if (j-1<1) or (b[i,j-1]=false) then ch:='u'; end;
'u':begin i:=i-1; a[i,j]:=t; b[i,j]:=false;
if (i-1<1) or (b[i-1,j]=false) then ch:='r'; end;
end;
until t=n*n;
for i:=1 to n do
begin
for j:=1 to n do
write(a[i,j]:4);
writeln;
end;
end.
4 楼
@左思@ [专家分:110] 发布于 2008-10-27 22:24:00
const n=5;
var s:array[0..n+1,0..n+1] of integer;
num,i,j,x,y,way:integer;
begin
for i:=1 to n do
for j:=1 to n do s[i,j]:=0;
for i:=0 to n+1 do
begin
s[0,i]:=1;
s[i,0]:=1;
s[n+1,i]:=1;
s[i,n+1]:=1;
end;
way:=1; num:=1; x:=1; y:=1; i:=0; j:=1; s[x,y]:=1;
while num<=n*n do
begin
if s[x+i,y+j]<>0 then
begin
inc(way);
if way=5 then way:=1;
case way of
1:begin i:=0; j:=1; end;
2:begin i:=1; j:=0; end;
3:begin i:=0; j:=-1; end;
4:begin i:=-1; j:=0; end;
end;
end;
inc(num);
x:=x+i; y:=y+j;
s[x,y]:=num;
end;
for i:=1 to n do
begin
for j:=1 to n do write(s[i,j]:4);
writeln;
end;
end.
数组s[..,..]保存填的数,n是方阵的规模(n*n),way是填的方向(1,2,3,4分别表示右,下,左,上),x,y记录填到数的坐标,num记录已经填的上一个数。
i,j是很巧妙的思想:
way i j
1 0 1
2 1 0
3 0 -1
4 -1 0
这样,用i,j对应方向(way),x+i,y+j就表示下一个要填数的坐标。
如果s[x+i,y+j]<>0,就表示这个位置有数或超出方阵边界(所以开始时要把方阵周围一圈填上1)。
我来回复