题目描述
blablabla
核心思想
和“平方矩阵I”一样采用递归方式。
1. 打印最外层
2. 递归打印中间层
C++ 代码
#include <iostream>
#include <cmath>
using namespace std;
const int N = 110;
void aux(int A[][N], int n, int level) {
if (n == 0) {
return;
}
if (n == 1) {
A[level][level] = 1;
return ;
}
// 打印最外层
for (int i = level; i != n + level; ++i) {
for (int j = level; j != n + level; ++j) {
if (i == level || i == n - 1 + level || j == level || j == n - 1 + level);
A[i][j] = abs(i - j) + 1;
}
}
// 递归打印中间部分
aux(A, n - 2, level + 1);
}
int main() {
int A[N][N];
int n;
while (cin >> n && n != 0) {
// 递归生成回形数组
aux(A, n, 0);
// 打印数组
for (int i = 0; i != n; ++i) {
for (int j = 0; j != n; ++j) {
cout << A[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
}