int i=5; 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 int i=6 1 2 3 4 5 6 20 21 22 23 24 7 19 32 33 34 25 8 18 31 36 35 26 9 17 30 29 28 27 10 16 15 14 13 12 11 要求按照以上规律打印数字 #include <stdio.h> #define N 100 int spiral_box[N][N]; int depth; static enum Direction{ Right, Down, Left, Up }lastDirection,direction; enum Direction FindthePosition(int row,int line) { direction = lastDirection; switch(direction) { case Right:{ if((line == depth-1)||(spiral_box[row][line+1]!=0)) direction = Down; break; } case Down:{ if((row == depth-1)||(spiral_box[row+1][line]!=0)) direction = Left; break; } case Left:{ if((line == 0)||(spiral_box[row][line-1]!=0)) direction = Up; break; } case Up:{ if((spiral_box[row-1][line]!=0)) direction = Right; break; } } return direction; } void print_spiral(unsigned int length) { int i,j; for(i=0;i<length;i++) { for(j=0;j<length;j++) { printf("%-3d ",spiral_box[i][j]); } printf("/n"); } } int main() { int i; int value = 1; int row = 0; int line = 0; lastDirection = Right; printf("please input the depth of spiral:"); scanf("%d",&depth); for(i=0;i<depth*depth;i++) { spiral_box[row][line] = value; lastDirection = FindthePosition(row,line); switch(lastDirection) { case Right:{line++;break;} case Down :{ row++;break;} case Left :{line--;break;} case Up :{ row--;break;} } value++; } print_spiral(depth); return 1; }