```c++
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,t=1;
cin>>n;
a[0][n-1]=t;
int x=0;
int y=n-1;
while(t<n*n)
{
while(a[x+1][y]==0&&x+1<n)
a[++x][y]=++t;//上下规律
while(a[x][y-1]==0&&y-1>=0)
a[x][--y]=++t;//右左规律
while(a[x-1][y]==0&&x-1>=0)
a[--x][y]=++t;//下上规律
while(a[x][y+1]==0&&y+1<n)
a[x][++y]==++t;//左右规律
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
return 0;
}
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/635045472666114.png "#left")