111
2024-11-27 19:02:46
发布于:浙江
0阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[40][40] = {0};
int row = 0, col = n / 2;
a[row][col] = 1;
for (int num = 2; num <= n * n; ++num) {
int newRow = (row - 1 + n) % n;
int newCol = (col + 1) % n;
if (a[newRow][newCol] == 0) {
row = newRow;
col = newCol;
} else {
row = (row + 1) % n;
}
a[row][col] = num;
}
for (int i = 0; i<n;i++){
for (int j = 0; j < n; ++j) {
cout << a[i][j] << " ";
}
cout << endl;
}
return 0;
}
这里空空如也
有帮助,赞一个