5
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
#define _CRT_SECURE_NO_WARNINGS #include#include using namespace std; int main() { int n; cin >> n; if (n % 2 == 0) { cout << "要求N 为奇数" << endl; return 0; } int* cube = new int[n * n]{0}; int x, y; x = 0; y = n / 2; cube[x * n + y] = 1; for (int k = 2; k <= n * n; k++) { if (x == 0 && y != n - 1) { x = n - 1; y += 1; } else if (x != 0 && y == n - 1) { x -= 1; y = 0; } else if (x == 0 && y == n - 1) { x += 1; } else if (x != 0 && y != n - 1) { if (cube[(x - 1) * n + (y + 1)] == 0) { x -= 1; y += 1; } else { x += 1; } } cube[x * n + y] = k; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << setw(4) << cube[i * n + j] << " "; } cout << "" << endl; } return 0; }



