1 #include<string.h> 2 #define maxn 20 3 int a[maxn][maxn]; 4 int main() 5 { 6 int n,x,y,tot = 0; 7 scanf("%d",&n); 8 memset(a,0,sizeof(a)); 9 tot = a[x=0][y=n-1] = 1; 10 while(tot < n*n) 11 { 12 while(x+1<n && ! a[x+1][y]) a[++x][y] = ++tot; 13 while(y-1>=0 && ! a[x][y-1]) a[x][--y] = ++tot; 14 while(x-1>=0 && ! a[x-1][y]) a[--x][y] = ++tot; 15 while(y+1<n && ! a[x][y+1]) a[x][++y] = ++tot; 16 } 17 for(x = 0;x<n;x++) 18 { 19 for(y=0;y<n;y++) printf("%3d",a[x][y]); 20 printf("\n"); 21 } 22 return 0; 23 }
蛇形填数
时间: 2024-09-30 23:58:12