编辑代码

#include <stdio.h>
#include <string.h>
#define maxn 200

int a[maxn][maxn];

int main() {
    int n, x, y, tot = 0;
    scanf("%d", &n);

    // Initialize the array to zero using memset
    memset(a, 0, sizeof(a));

    // Starting position for the spiral
    x = 0;
    y = n - 1;
    a[x][y] = ++tot;

    while (tot < n * n) {
        // Move downwards if possible
        while (x + 1 < n && !a[x + 1][y]) a[++x][y] = ++tot;

        // Move leftwards if possible
        while (y - 1 >= 0 && !a[x][y - 1]) a[x][--y] = ++tot;

        // Move upwards if possible
        while (x - 1 >= 0 && !a[x - 1][y]) a[--x][y] = ++tot;

        // Move rightwards if possible
        while (y + 1 < n && !a[x][y + 1]) a[x][++y] = ++tot;
    }

    // Print the result
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            printf("%d ", a[i][j]);
        }
        printf("\n");
    }

    return 0;
}