#include <iostream> #include <cstring> using namespace std; void (int *A, int n); int main() { int *A; int n; while (cin >> n) { A = new int[n*n]; memset(A, 0, n*n * sizeof(int)); int row, col; row = 0; col = (n - 1) / 2; A[row*n + col] = 1; for (int k = 2; k <= n*n; k++) { row--; col++; if (row < 0) row += n; if (col >= n) col -= n; while (A[row*n + col] != 0) { row += 2; col--; if (row >= n) row -= n; if (col < 0) col += n; } A[row*n + col] = k; } DisplayMatrix(A, n); delete[] A; } } void (int *A, int n) { for (int row = 0; row < n; ++row) { for (int col = 0; col < n; ++col) { cout.width(5); cout << A[row*n + col]; } cout << endl; } }
|
近期评论