![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 //蛇形矩阵 2 #include <stdio.h> 3 #define N 5 4 5 int main() 6 { 7 int i, j, k, a[N][N]; 8 int circle = 0; 9 for(i=0; i<N; i++) 10 for(j=0; j<N; j++) 11 a[i][j] = 0; 12 i = 0; j = 0; k=1; 13 while(circle <= N/2) 14 { 15 if(i==(0+circle) && j==(0+circle)) 16 { 17 while(j<N-circle) a[i][j++] = k++; 18 i=1+circle;j=N-1-circle; 19 } 20 if(i==(1+circle) && j==(N-1-circle)) 21 { 22 while(i<N-circle) a[i++][j] = k++; 23 i=N-1-circle; j=N-2-circle; 24 } 25 if(i==(N-1-circle) && j==(N-2-circle)) 26 { 27 while(j>=0+circle)a[i][j--] = k++; 28 i=N-2-circle; j=0+circle; 29 } 30 if(i==(N-2-circle) && j==(0+circle)) 31 { 32 while(i>=1+circle) a[i--][j] = k++; 33 circle++; 34 i=circle; j=circle; 35 } 36 } 37 printf("\n"); 38 for(i=0; i<N; i++) 39 { 40 printf("\t"); 41 for(j=0; j<N; j++) 42 printf("%4d", a[i][j]); 43 printf("\n"); 44 } 45 return 0; 46 }