题目描述
blablabla
核心想法
- 最外层输出一圈1
- 递归输出中间部分
- 中间部分还要加1
C++ 代码
#include <iostream>
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 ;
}
// 最外层套一层1
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] = 1;
}
}
// 递归填充中间部分
aux(A, n - 2, level + 1);
// 中间部分加1
for (int i = level + 1; i != n - 1 + level; ++i) {
for (int j = level + 1; j != n - 1 + level; ++j) {
A[i][j] += 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;
}
}