文件目录

#include <iostream>
using namespace std;
int main()
{
    int n;
    cin >> n;
    int a[25][25] = {};
    int x = 1, y = n, i = 1;
    a[x][y] = i;
    while (i < n * n)
    {
        while (x + 1 <= n && a[x + 1][y] == 0)
            a[++x][y] = ++i;
        while (y - 1 >= 1 && a[x][y - 1] == 0)
            a[x][--y] = ++i;
        while (x - 1 >= 1 && a[x - 1][y] == 0)
            a[--x][y] = ++i;
        while (y + 1 <= n && a[x][y + 1] == 0)
            a[x][++y] = ++i;
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}