螺旋三角 PASCAL
发布网友
发布时间:2024-10-15 16:55
我来回答
共1个回答
热心网友
时间:2024-11-17 03:01
program ;{螺旋三角形}
var
a:array[1..20,1..20]of byte;
x,y,i,j,n:byte;
function ok(x,y:byte):boolean;
begin
ok:=false;
if (x<=n)and(y<=n)then
if i<=n*(n+1)div 2 then
if a[x,y]=0 then
ok:=true;
end;
BEGIN
repeat
readln(n);
until (n>0)and(n<=20);
i:=1;x:=0;y:=1;
fillchar(a,sizeof(a),0);
repeat
while ok(x+1,y)do
begin inc(x);a[x,y]:=i;inc(i);end;
while ok(x,y+1)do
begin inc(y);a[x,y]:=i;inc(i);end;
while ok(x-1,y-1)do
begin dec(x);dec(y);a[x,y]:=i;inc(i);end;
until i>n*(n+1)div 2;
for i:=1 to n do
begin
write('':2*(n-i));
for j:=1 to i do write(a[i,j]:4);
writeln
end;
END.