Posted on 2014-07-19 12:25
xLsDg 阅读(244)
评论(0) 编辑 收藏 引用 所属分类:
代码库
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #define MAX_RANGE 10
5
6 int main()
7 {
8 char direction;
9 int num[MAX_RANGE][MAX_RANGE], i, j, k;
10
11 for ( i = 0; i < MAX_RANGE; i++ ) {
12 for ( j = 0; j < MAX_RANGE; j++ ) {
13 num[i][j] = -1;
14 }
15 }
16
17 for ( i = 0, j = 0, k = 0, direction = 0; k < MAX_RANGE * MAX_RANGE; k++ ) {
18 if ( 0 <= i && i < MAX_RANGE && 0<= j && j < MAX_RANGE && -1 == num[i][j] ) {
19
20 } else {
21 switch ( direction ) {
22 case 0:
23 i--;
24 j++;
25 direction = 1;
26 break;
27 case 1:
28 i--;
29 j--;
30 direction = 2;
31 break;
32 case 2:
33 i++;
34 j--;
35 direction = 3;
36 break;
37 case 3:
38 i++;
39 j++;
40 direction = 0;
41 break;
42 }
43
44 }
45
46 num[i][j] = k;
47
48 switch ( direction ) {
49 case 0:
50 i++;
51 break;
52 case 1:
53 j++;
54 break;
55 case 2:
56 i--;
57 break;
58 case 3:
59 j--;
60 break;
61 }
62 }
63
64 for ( i = 0; i < MAX_RANGE; i++ ) {
65 for ( j = 0; j < MAX_RANGE; j++ ) {
66 printf("%5d ", num[i][j] );
67 }
68 printf("\n");
69 }
70
71 return 0;
72 }
73