@[yuan2795](/user/1200551)
求关
```pascal
#include <bits/stdc++.h>
using namespace std;
int n;
int a[10][10];
int b;
int vis[5][2]={{0,1},{1,0},{0,-1},{-1,0}};
int main()
{
int x=1;
int y=1;
cin>>n;
int k=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
k++;
a[x][y]=k;
if(k==n*n)
{
break;
}
int w=x+vis[b][0];
int cu=y+vis[b][1];
if(!(w>=1&&w<=n&&cu>=1&&cu<=n&&a[w][cu]==0))
{
b=(b+1)%4;
}
x+=vis[b][0];
y+=vis[b][1];
//cout<<a[x][y];
}
//cout<<endl;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cout<<" ";
if(a[i][j]<10)
{
cout<<" ";
}
cout<<a[i][j];
}
cout<<endl;
}
}
```
by Yxy7952 @ 2024-09-04 14:03:30
?
by yuan2795 @ 2024-09-04 22:03:43
数组处理部分没看,但是输出部分,题目说了每个数占3个字符(不再需要加空格)
要printf("%3d",a[i][j]);
by Mys_Ept @ 2024-09-14 18:03:26