我在以前做python时就做过这个,非常有趣,废话不多说,直接上代码。
# include <iostream>
# include <cstring>
using namespace std;
int main(){
int a;
cin >> a;
int sz1[a][a], h = 0, l = int(a / 2 - 0.5 + 1);
memset(sz1, 0, sizeof(sz1));
sz1[0][int(a / 2 - 0.5 + 1)] = 1;
for (int k = 2; k <= a * a; k++){
h--;
l++;
if (h == -1 && l != a){
h = a - 1;
}
if (l == a && h != -1){
l = 0;
}
if (sz1[h][l] != 0){
h += 2;
l--;
}
if (h == -1 && l == a){
h += 2;
l--;
}
sz1[h][l] = k;
}
for (int m = 0; m < a; m++){
for (int n = 0; n < a; n++){
printf("%-4d ", sz1[m][n]);
}
cout << "\n";
}
}