首页 > 代码库 > 1807: 【基础】螺旋方阵

1807: 【基础】螺旋方阵

program ex1807;
var n,i,j,t,f,l,q,z:integer;s1,s2:longint;
a:array[1..20,1..20] of integer;
begin
readln(n);
t:=1; f:=1; l:=n;i:=1;q:=1;z:=n;
while t<=n*n do
begin
case f of
1:begin
for j:=q to z do begin a[i,j]:=t;inc(t);end;
f:=2; q:=i+1;dec(l); z:=l+q-1;
end;
2:begin
for i:=q to z do begin a[i,j]:=t;inc(t);end;
f:=3;q:=j-1; z:=q-l+1;
end;
3:begin
for j:=q downto z do begin a[i,j]:=t;inc(t); end;
f:=4;q:=i-1;dec(l);z:=q-l+1;
end;
4:begin
for i:=q downto z do begin a[i,j]:=t;inc(t); end;
f:=1;q:=j+1;z:=q+l-1;
end;
end;
end;
s1:=0;s2:=0;
for i:=1 to n do s1:=s1+a[i,i];
for i:=n downto 1 do s2:=s2+a[n-i+1,i];
writeln(s1-s2);
end.

1807: 【基础】螺旋方阵