<span style="font-size:18px;">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXN 50
int a[MAXN][MAXN];
int main()
{
int n;
int x,y;
int total;
while(~scanf("%d",&n))
{
total = 0;
x = 0; y = n-1;
memset(a,0,sizeof(a));
total = a[x][y] = 1;
while(total<n*n)
{
while(x<n-1 && !a[x+1][y]) a[++x][y] = ++total;
while(y>0 && !a[x][y-1]) a[x][--y] = ++total;
while(x>0 && !a[x-1][y]) a[--x][y] = ++total;
while(y<n-1 && !a[x][y+1]) a[x][++y] = ++total;
}
for(x=0; x<n; x++)
{
for(y=0; y<n; y++)
{
printf("%4d ",a[x][y]);
}
putchar('\n');
}
}
return 0;
}
</span>
蛇形填数
最新推荐文章于 2024-05-26 11:37:30 发布