一层层覆盖应该是最直观的
拿 $n = 5$ 举例。首先计算出需要覆盖 int(0.5*(n+1)) = 3 次。
第一次覆盖,将整个5x5正方形赋值为1;第二次覆盖,把中间3x3的正方形赋值为2......
#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
while (n != 0){
int a[n][n];
for (int t = 0; t < int(0.5*(n+1)); t ++){
for (int i = t; i < n - t; i ++ ){
for (int j = t; j < n - t; j ++ ){
a[i][j] = t + 1;
}
}
}
for (int i = 0; i < n; i ++){
for (int j = 0; j < n; j ++){
cout << a[i][j] << " ";
}
cout << endl;
}
cout << endl;
cin >> n;
}
return 0;
}