蛇形打印

 1 #include<cstdio>
 2 #include<cstring>
 3 const int N = 10;
 4 int a[N][N];
 5 int main(){
 6     int n,x,y,tot;    
 7     memset(a,0,sizeof(a));
 8     scanf("%d",&n);//n行n列 
 9 
10     tot = a[x=0][y=n-1]=1;
11     
12     while(tot<n*n){
13         while(y+1<n&&!a[x][y+1])
14             a[x][++y]=++tot;
15         while(x+1<n&&!a[x+1][y])
16             a[++x][y]=++tot;
17         while(y-1>=0&&!a[x][y-1])
18             a[x][--y]=++tot;
19         while(x-1>=0&&!a[x-1][y])
20             a[--x][y]=++tot;
21     }
22     for(x=0;x<n;x++){
23         for(y=0;y<n;y++)
24             printf("%-3d",a[x][y]);
25         printf("\n");
26     }
27     
28     return 0;
29 }

 

posted @ 2020-03-08 22:28  xuecl  阅读(235)  评论(0编辑  收藏  举报