提交时间:2024-05-05 10:00:43

运行 ID: 39710

#include<bits/stdc++.h> using namespace std; int n,s[105][105],x,y,t; int main(){ cin>>n; x=1; y=n; t=n*n; s[x][y]=t; t--; while(t){ while(x+1<=n&&s[x+1][y]==0){ x++; s[x][y]=t; t--; } while(y-1>0&&s[x][y-1]==0){ y--; s[x][y]=t; t--; } while(x-1>0&&s[x-1][y]==0){ x--; s[x][y]=t; t--; } while(y+1<=n&&s[x][y+1]==0){ y++; s[x][y]=t; t--; } } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ cout<<s[i][j]<<" "; } cout<<endl; } return 0; }