@[Do_Do_Fish](/user/892520) 建议改成这样:
```cpp
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n = 0;
cin >> n;
int arr[100] = {};
int p = n;
if (n == 1)
arr[1] = 1;
else if (n == 2)
{
arr[1] = 1;
arr[2] = 2;
arr[3] = 4;
arr[4] = 3;
}
else
{
int m = 1;
int x = 1;
int y = 1;
int q = 1;
int begin = 1;
for(int i=1;i<=n*n;p=p-2)
{
for ( m = 0; m < p&&i<=n*n; m++)
{
arr[m+begin] = i;
i++;
}
for ( x = 1; x <= p - 1 && i <= n * n; x++)
{
arr[p-1+begin+ n* x] = i;
i++;
}
for (y = 1; y <= p - 1 && i <= n * n; y++)
{
arr[p-1 + begin + n * (p-1) - y] = i;
i++;
}
for (q = 1; q <= p - 2 && i <= n * n; q++)
{
arr[p - 1 + begin + (n-1) * (p - 1) - q * n] = i;
i++;
}
begin = p - 1 + begin + (n-1)*(p-1) - (p-2) * n+1;
}
}
for (int i = 1; i <= n*n; i++)
{
if (arr[i] <= 9 && i % n == 1)
cout << setw(3) << arr[i];
else if (arr[i] <= 9 && i % n != 1)
cout <<setw(3)<< arr[i];
else if (arr[i] > 9 && i % n == 1)
cout <<setw(3)<< arr[i];
else
cout <<setw(3) << arr[i];
if (i % n == 0&&i!=n*n)
cout << endl;
}
}
```
题目中说每个数字有都会占用 $3$ 个字符,所以要加上 `setw(3)` ~~求关注 QWQ~~
by Lizj @ 2022-12-22 13:26:43
@[Mr__AC](/user/762010)
过了!非常感谢!!
by Do_Do_Fish @ 2022-12-22 23:18:28