1 program Project21;
2
3 {$APPTYPE CONSOLE}
4
5 uses
6 SysUtils;
7
8 procedure Draw(Size:Integer);
9 var
10 Arr:Array of Array of Integer;
11 Flag:(GoRight,GoButtom,GoLeft,GoTop);
12 X,Y,N,Step,Rang:Integer;
13 procedure Output;
14 begin
15 Inc(N);
16 Arr[Y,X]:=N;
17 Dec(Step);
18 end;
19 begin
20 N:=0; X:=0; Y:=0;
21 Rang:=Size-1; Step:=Rang;
22 SetLength(Arr,Size,Size);
23 while N<Size*Size do begin
24 case Flag of
25 GoRight:
26 begin
27 if Step=0 then begin
28 Step:=Rang;
29 Flag:=GoButtom;
30 end else begin
31 Output;
32 Inc(X);
33 end;
34 end;
35 GoButtom:
36 begin
37 if Step=0 then begin
38 Step:=Rang;
39 Flag:=GoLeft;
40 end else begin
41 Output;
42 Inc(Y);
43 end;
44 end;
45 GoLeft:
46 begin
47 if Step=0 then begin
48 Step:=Rang;
49 Flag:=GoTop;
50 end else begin
51 Output;
52 Dec(X);
53 end;
54 end;
55 GoTop:
56 begin
57 if Step=0 then begin
58 Dec(Rang,2);
59 Step:=Rang;
60 Flag:=GoRight;
61 Inc(X);
62 Inc(Y);
63 if Rang<=0 then begin
64 Output;
65 Break;
66 end;
67 end else begin
68 Output;
69 Dec(Y);
70 end;
71 end;
72 end;
73 end;
74 for Y := 0 to Size-1 do begin
75 for X := 0 to Size-1 do
76 Write(Format('%4d',[Arr[Y,X]]));
77 Writeln('');
78 end;
79 end;
80
81 var
82 Size:String;
83 label
84 Start;
85 begin
86 try
87 Start:
88 Write('输入阵列的规模:');
89 Readln(Size);
90 Draw(StrToIntDef(Size,0));
91 goto Start;
92 except
93 on E: Exception do
94 Writeln(E.ClassName, ': ', E.Message);
95 end;
96 end.
请发表评论