Files
python/TangDou/LuoGuBook/P2615.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

34 lines
716 B
C++

#include <bits/stdc++.h>
using namespace std;
const int N = 40;
int n;
int g[N][N];
int main() {
cin >> n;
int r = 1;
int c = n / 2 + 1;
g[r][c] = 1;
for (int i = 2; i <= n * n; i++) {
if (r == 1 && c != n)
r = n, c++;
else if (c == n && r != 1)
r--, c = 1;
else if (r == 1 && c == n)
r = 2;
else if (r != 1 && c != n) {
if (g[r - 1][c + 1] == 0)
r--, c++;
else
r++;
}
g[r][c] = i;
}
for (int i = 1; i <= n; i++, puts(""))
for (int j = 1; j <= n; j++)
printf("%d ", g[i][j]);
return 0;
}