幻方解法(用了while循环,有需要的可以自己祛除)
#include <bits/stdc++.h>
using namespace std;
int main() {
while (114514) {
long long n;
cin >> n;
long long x = 0, y = ceil(n / 2);
long long a[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = 0;
}
}
for (int i = 1; i <= n * n; i++) {
a[x][y] += i;
x--;
y++;
if (x < 0 && y < n) {
x += n;
}
else if (y >= n && x >= 0) {
y -= n;
}
else if(a[x][y] != 0 || x < 0 && y >= n) {
x += 2;
y--;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%lld ", a[i][j]);
}
printf("\n");
}
}
}
本人不定期更新日常小代码(大佬勿喷,在下蒟蒻)