Run ID:158325

提交时间:2026-08-20 18:19:27

#include <bits/stdc++.h> using namespace std; int main(){ int N; cin>>N; int a[101][101]; int top=0,bottom=N-1,left=0,right=N-1; int n=1; while(n<=N*N){ for(int i=top;i<=bottom && n<=N*N;i++){ a[i][right]=n; n++; } right--; for(int i=right;i>=left && n<=N*N;i--){ a[bottom][i]=n; n++; } bottom--; for(int i=bottom;i>=top && n<=N*N;i--){ a[i][left]=n; n++; } left++; for(int i=left;i<=right && n<=N*N;i++){ a[top][i]=n; n++; } top++; } for(int i=0;i<N;i++){ for(int j=0;j<N;j++){ cout<<a[i][j]<<" "; } cout<<endl; } return 0; }