#include <iostream>
#include <vector>
using namespace std;
#include<iomanip>
int n;
int main()
{
cin >> n;
vector<vector<int>> arr(n, vector<int>(n, 0));
int x = 0, y = 0, s = 1;
while (s <= n * n)
{
while (y < n && arr[x][y] == 0)
{
arr[x][y++] = s++;
}
y--; x++;
while (x < n && arr[x][y] == 0)
{
arr[x++][y] = s++;
}
x--; y--;
while (y >= 0 && arr[x][y] == 0)
{
arr[x][y--] = s++;
}
y++; x--;
while (x >= 0 && arr[x][y] == 0)
{
arr[x--][y] = s++;
}
x++; y++;
}
for (int u = 0; u < n; u++)
{
for (int i = 0; i < n; i++)
{
cout << setw(3) << arr[u][i];
}
cout << endl;
}
return 0;
}
标签:arr,++,while,C++,--,int,&&,方阵,解法 From: https://blog.csdn.net/yagma_tunita/article/details/142908107