include
using namespace std; int main() { int n; cin>>n; int a[n+1][n+1]; memset(a,0,sizeof(a)); int count=1; int x=1,y=1; a[x][y]=count; count++; while(count<=n*n) { while(a[x][y+1]==0&&y+1<=n) { y++; a[x][y]=count++; } //向右填 while(a[x+1][y]==0&&x+1<=n) { x++; a[x][y]=count++; } //向下填 while(a[x][y-1]==0&&y-1>=1) { y--; a[x][y]=count++; } //向左填 while(a[x-1][y]==0&&x-1>=1) { x--; a[x][y]=count; count++; } //向上填 } for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) cout<<setw(4)<<a[i][j]; cout<<endl; } return 0; }