蛇形添数
#include <stdio.h>
#include <string.h>
int main(void)
{
int arr[100][100];
memset(arr,0,sizeof(arr));
//都初始为0
int n,x=0,y=0,i,j,tot=0;
scanf("%d",&n);
y=n-1;
arr[0][n-1]=1;
tot=1;
while (tot<n*n)
{
while(x<n-1&&!arr[x+1][y]){arr[++x][y]=++tot;}
//前一位有非0时转向
while(y-1>=0&&!arr[x][y-1]){arr[x][--y]=++tot;};
//边界值
while(x-1>=0&&!arr[x-1][y]){arr[--x][y]=++tot;}
while(y+1<n&&!arr[x][y+1]){arr[x][++y]=++tot;}
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
return 0;
}