打印下列图形成 用PASCAL做

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.