Run ID:93116

提交时间:2024-10-10 15:33:34

#include<cstdio> #include<cstring> #define maxn 21 int a[maxn][maxn]; int main() { int n,x,y,tot; scanf("%d",&n); memset(a,0,sizeof(a)); tot=a[x=0][y=0]=1; while(tot<n*n){ while(x+1<n&&!a[x+1][y]) a[++x][y]=++tot; a[x][++y]=++tot; while(x-1>=0&&!a[x-1][y]) a[--x][y]=++tot; a[x][++y]=++tot; } for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ printf("%3d",a[i][j]); } printf("\n"); } return 0; }