1
/*
2 @2011/6/14 螺旋方阵
3 @author zengweilin
4 */
5 #include " stdio.h "
6 #include " stdlib.h "
7
8 void widdershins( int );
9 void deasil( int );
10
11 int main(){
12 int n;
13 printf( " please input the value of n: " );
14 scanf( " %d " , & n);
15 while (n <= 0 )
16 {
17 printf( " error! " );
18 printf( " \nplease input the value of n: " );
19 scanf( " %d " , & n);
20 }
21 printf( " the widdershins matrix:\n " );
22 widdershins(n);
23 printf( " the deasil matrix:\n " );
24 deasil(n);
25 return 0 ;
26 }
27 /* 螺旋方阵函数(逆时针) */
28 void widdershins( int n)
29 {
30 int i,j,k = 1 ,m;
31 int ** p;
32
33 /* 动态内存分配 */
34 p = ( int ** )calloc(n, sizeof ( int ));
35 for (i = 0 ;i < n;i ++ )
36 {
37 p[i] = ( int * )calloc(n, sizeof ( int ));
38 }
39
40 if (n == 1 )
41 {
42 printf( " \t1\t " );
43 printf( " \n " );
44 }
45 else
46 {
47 m = (n % 2 == 0 ) ? n / 2 :n / 2 + 1 ;
48 for (i = 0 ;i < m;i ++ )
49 {
50 // 圈的左边
51 for (j = i;j < n - i;j ++ )
52 {
53 p[j][i] = k;
54 k ++ ;
55 }
56 // 圈的下边
57 for (j = i + 1 ;j < n - i;j ++ )
58 {
59 p[n - i - 1 ][j] = k;
60 k ++ ;
61 }
62 // 圈的右边
63 for (j = n - i - 2 ;j >= i;j -- )
64 {
65 p[j][n - i - 1 ] = k;
66 k ++ ;
67 }
68 // 圈的上边
69 for (j = n - i - 2 ;j >= i + 1 ;j -- )
70 {
71 p[i][j] = k;
72 k ++ ;
73 }
74 }
75 }
76 for (i = 0 ;i < n;i ++ )
77 {
78 for (j = 0 ;j < n;j ++ )
79 {
80 printf( " %5d " ,p[i][j]);
81 }
82 printf( " \n " );
83 }
84 }
85 /* 螺旋方阵函数(顺时针) */
86 void deasil( int n)
87 {
88 int i,j,k = 1 ,m;
89 int ** p;
90
91 /* 动态内存分配 */
92 p = ( int ** )calloc(n, sizeof ( int ));
93 for (i = 0 ;i < n;i ++ )
94 {
95 p[i] = ( int * )calloc(n, sizeof ( int ));
96 }
97
98 if (n == 1 )
99 {
100 printf( " \t1\t " );
101 printf( " \n " );
102 }
103 else
104 {
105 m = (n % 2 == 0 ) ? n / 2 :n / 2 + 1 ;
106 for (i = 0 ;i < m;i ++ )
107 {
108 // 圈的上边
109 for (j = i;j < n - i;j ++ )
110 {
111 p[i][j] = k;
112 k ++ ;
113 }
114 // 圈的右边
115 for (j = i + 1 ;j < n - i;j ++ )
116 {
117 p[j][n - i - 1 ] = k;
118 k ++ ;
119 }
120 // 圈的下边
121 for (j = n - i - 2 ;j >= i;j -- )
122 {
123 p[n - i - 1 ][j] = k;
124 k ++ ;
125 }
126 // 圈的左边
127 for (j = n - i - 2 ;j >= i + 1 ;j -- )
128 {
129 p[j][i] = k;
130 k ++ ;
131 }
132 }
133 }
134 for (i = 0 ;i < n;i ++ )
135 {
136 for (j = 0 ;j < n;j ++ )
137 {
138 printf( " %5d " ,p[i][j]);
139 }
140 printf( " \n " );
141 }
142 }
2 @2011/6/14 螺旋方阵
3 @author zengweilin
4 */
5 #include " stdio.h "
6 #include " stdlib.h "
7
8 void widdershins( int );
9 void deasil( int );
10
11 int main(){
12 int n;
13 printf( " please input the value of n: " );
14 scanf( " %d " , & n);
15 while (n <= 0 )
16 {
17 printf( " error! " );
18 printf( " \nplease input the value of n: " );
19 scanf( " %d " , & n);
20 }
21 printf( " the widdershins matrix:\n " );
22 widdershins(n);
23 printf( " the deasil matrix:\n " );
24 deasil(n);
25 return 0 ;
26 }
27 /* 螺旋方阵函数(逆时针) */
28 void widdershins( int n)
29 {
30 int i,j,k = 1 ,m;
31 int ** p;
32
33 /* 动态内存分配 */
34 p = ( int ** )calloc(n, sizeof ( int ));
35 for (i = 0 ;i < n;i ++ )
36 {
37 p[i] = ( int * )calloc(n, sizeof ( int ));
38 }
39
40 if (n == 1 )
41 {
42 printf( " \t1\t " );
43 printf( " \n " );
44 }
45 else
46 {
47 m = (n % 2 == 0 ) ? n / 2 :n / 2 + 1 ;
48 for (i = 0 ;i < m;i ++ )
49 {
50 // 圈的左边
51 for (j = i;j < n - i;j ++ )
52 {
53 p[j][i] = k;
54 k ++ ;
55 }
56 // 圈的下边
57 for (j = i + 1 ;j < n - i;j ++ )
58 {
59 p[n - i - 1 ][j] = k;
60 k ++ ;
61 }
62 // 圈的右边
63 for (j = n - i - 2 ;j >= i;j -- )
64 {
65 p[j][n - i - 1 ] = k;
66 k ++ ;
67 }
68 // 圈的上边
69 for (j = n - i - 2 ;j >= i + 1 ;j -- )
70 {
71 p[i][j] = k;
72 k ++ ;
73 }
74 }
75 }
76 for (i = 0 ;i < n;i ++ )
77 {
78 for (j = 0 ;j < n;j ++ )
79 {
80 printf( " %5d " ,p[i][j]);
81 }
82 printf( " \n " );
83 }
84 }
85 /* 螺旋方阵函数(顺时针) */
86 void deasil( int n)
87 {
88 int i,j,k = 1 ,m;
89 int ** p;
90
91 /* 动态内存分配 */
92 p = ( int ** )calloc(n, sizeof ( int ));
93 for (i = 0 ;i < n;i ++ )
94 {
95 p[i] = ( int * )calloc(n, sizeof ( int ));
96 }
97
98 if (n == 1 )
99 {
100 printf( " \t1\t " );
101 printf( " \n " );
102 }
103 else
104 {
105 m = (n % 2 == 0 ) ? n / 2 :n / 2 + 1 ;
106 for (i = 0 ;i < m;i ++ )
107 {
108 // 圈的上边
109 for (j = i;j < n - i;j ++ )
110 {
111 p[i][j] = k;
112 k ++ ;
113 }
114 // 圈的右边
115 for (j = i + 1 ;j < n - i;j ++ )
116 {
117 p[j][n - i - 1 ] = k;
118 k ++ ;
119 }
120 // 圈的下边
121 for (j = n - i - 2 ;j >= i;j -- )
122 {
123 p[n - i - 1 ][j] = k;
124 k ++ ;
125 }
126 // 圈的左边
127 for (j = n - i - 2 ;j >= i + 1 ;j -- )
128 {
129 p[j][i] = k;
130 k ++ ;
131 }
132 }
133 }
134 for (i = 0 ;i < n;i ++ )
135 {
136 for (j = 0 ;j < n;j ++ )
137 {
138 printf( " %5d " ,p[i][j]);
139 }
140 printf( " \n " );
141 }
142 }