代码:
#include <iostream>
using namespace std;
int l[1001][1001];
int main() {
int n;
cin >> n;
int y = 0, x = n / 2 + 1;
l[y][x] = 1;
for (int i = 2; i <= n * n; ++i) {
if (y == 0 && x != n - 1) {
y = n - 1;
x ++;
} else if (x == n - 1 && y != 0) {
y = 0;
x --;
} else if (y == 0 && x == n - 1) {
y ++;
} else if (y != 0 && x != n - 1) {
if(l[x+1][y-1] != 0){
y --;
x ++;
}
else{
y++;
}
}
l[y][x] = i;
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << l[i][j] << " ";
}
cout << "\n";
}
return 0;
}
为什么输出是:
0 9 1
0 0 2
0 0 8
求大佬帮助TwT
Thanks♪(・ω・)ノ