View code
1 # Include <iostream>
2 # Include <iomanip>
3 // # Include "checkmemoryleak. H"
4
5 Using Namespace STD;
6
7 Void Spiral ( Int N)
8 {
9 Int Temp = 0 ; // Record Layers
10 Int Num = 1 ; // Enter the array value
11 Int I = 0 , J = 0 ; // Control Loop
12 Int X = 0 , Y =- 1 ; // Control direction
13 Int ** N =New Int * [N]; // Array
14 For (I = 0 ; I <n; I ++)
15 {
16 N [I] = New Int [N];
17 }
18 While (Temp <= N)
19 {
20 For (I = 0 ; I <n-temp; I ++)
21 {
22 N [x] [++ y] = num ++;
23 }
24 If (Temp ++, temp = N)
25 Break ;
26 For (I = 0 ; I <n-temp; I ++)
27 {
28 N [++ x] [Y] = num ++;
29 }
30 For (I = 0 ; I <n-temp; I ++)
31 {
32 N [x] [-- y] = num ++;
33 }
34 If (Temp ++, temp = N)
35 Break ;
36 For (I = 0 ; I <n-temp; I ++)
37 {
38 N [-- X] [Y] = num ++;
39 }
40 }
41 For (I = 0 ; I <n; I ++)
42 {
43 For (J = 0 ; J <n; j ++)
44 {
45 Cout <SETW ( 3 ) <N [I] [J];
46 }
47 Cout <Endl;
48 }
49 For (I = 0 ; I <n; I ++)
50 {
51 Delete [] n [I];
52 }
53 Delete [] N;
54 }
55 Int Main ()
56 {
57 // Enablememleakcheck ();
58 Int N = 0 ;
59 Cout < " Enter the value of integer N: " <Endl;
60 Cin> N;
61 Spiral (N );
62 Return 0 ;
63 }
Running result:
Please input n
5
Output:
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9