@[IAKIOI14141](/user/950713) 仔细看题
by laialaodi @ 2024-09-22 14:06:40
`注意每个数字有都会占用
3
3 个字符,前面使用空格补齐。` @[IAKIOI14141](/user/950713)
by laialaodi @ 2024-09-22 14:07:17
**acode**:
```cpp
#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 zhuyanyv @ 2024-09-24 20:23:30
不小心写错了,看这个:
```cpp
#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 zhuyanyv @ 2024-09-24 20:24:53
@[laialaodi](/user/713554)
thank you
by IAKIOI14141 @ 2024-10-07 14:24:23
@[zhuyanyv](/user/1458517)
thank you too
by IAKIOI14141 @ 2024-10-07 14:24:54