#include<iostream>
using namespace std;
int main() {
int n = 0, cnt = 1, x = 1, y = 0;
int arr[15][15];
cin >> n;
while (cnt <= n * n)
{
for (; y < n && !arr[x][y + 1]; cnt++)
arr[x][++y] = cnt;
for (; x < n && !arr[x + 1][y]; cnt++)
arr[x++][y] = cnt;
for (; y > 1 && !arr[x][y - 1]; cnt++)
arr[x][--y] = cnt;
for (; x > 1 && !arr[x - 1][y]; cnt++)
arr[--x][y] = cnt;
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
}
输入之后按回车没反应
参照了题解的解法用for写的