题目原文请移步下面的链接
模拟
普及-
#include <bits/stdc++.h>
using namespace std;
#define endl '\n';
void best_coder() {
int n;
scanf("%d", &n);
vector<vector<int>> g(n, vector<int>(n));
g[0][(n - 1) / 2] = 1;
pair<int, int> k = {0, (n - 1) / 2};
for (int i = 2; i <= n * n; ++i) {
if(k.first == 0 && k.second != n - 1) {
g[n - 1][k.second + 1] = i;
k.first = n - 1;
++k.second;
} else if(k.first != 0 && k.second == n - 1) {
g[k.first - 1][0] = i;
--k.first;
k.second = 0;
} else if(k.first == 0 && k.second == n - 1) {
g[1][n - 1] = i;
++k.first;
} else if(k.first != 0 && k.second != n - 1) {
if (g[k.first - 1][k.second + 1] == 0) {
g[k.first - 1][k.second + 1] = i;
--k.first;
++k.second;
} else {
g[k.first + 1][k.second] = i;
++k.first;
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
printf("%d%s", g[i][j], " ");
}
printf("\n");
}
}
void happy_coder() {
}
int main() {
// 提升cin、cout效率
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// 小码匠
best_coder();
// 最优解
// happy_coder();
// 返回
return 0;
}
END