由一道数学题的联想
然后根据网上的做法瞎jb乱打了一下,居然对了
代码精心附上了注释,有兴趣的童鞋可以看一看。。
不说了,上代码!(自认为结构很清晰易懂)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
| #include <cstdlib> #include <cmath> #include <cstdio> #define MAXN 10000 using namespace std;
int matrix[MAXN][MAXN] = { 0 };
void (int n) { int x = 0, y, mun = 1; y = n / 2; while (mun <= n * n) { matrix[x][y] = mun;
int x0 = x; int y0 = y; x0--; y0++; if (x0 < 0) x0 += n; if (y0 == n) y0 = n - y0; if (!matrix[x0][y0]) { x = x0; y = y0; } else { x++; if (x == n) x = 0; } mun++; } }
void CreateDoubleEvenMagicSqure(int n) { int num = 1; for (int i = 0; i<n; i++) for (int j = 0; j<n; j++) matrix[i][j] = num++;
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { if (i % 4 == 0 && 大专栏 C++求解N阶幻方an class="built_in">abs(i - j) % 4 == 0) for (int k = 0; k<4; k++) matrix[i + k][j + k] = abs(n * n + 1 - matrix[i + k][j + k]); else if (i % 4 == 3 && (i + j) % 4 == 3) for (int k = 0; k<4; k++) matrix[i - k][j + k] = abs(n * n + 1 - matrix[i - k][j + k]);
} }
void CreateSingleEvenMagicSqure(int n) { int k = n / 2; CreateOddMagicSquare(k); for (int i = 0; i < k; i++) for (int j = 0; j < k; j++) { matrix[i + k][j + k] = matrix[i][j] + k * k; matrix[i][j + k] = matrix[i][j] + k * k * 2; matrix[i + k][j] = matrix[i][j] + k * k * 3; } int m = (n - 2) / 4; for (int i = 0; i<m - 1; i++) { int buf = matrix[k / 2][i]; matrix[k / 2][i] = matrix[k / 2 + k][i]; matrix[k / 2 + k][i] = buf; } int buf = matrix[k / 2][k / 2]; matrix[k / 2][k / 2] = matrix[k / 2 + k][k / 2]; matrix[k / 2 + k][k / 2] = buf;
for (register int i = 0; i<k; i++) for (register int j = 0; j<k / 2; j++) { if (i != k / 2) { int buf = matrix[i][j]; matrix[i][j] = matrix[i + k][j]; matrix[i + k][j] = buf; } }
for (register int i = 0; i < k; i++) for (register int j = n - 1; j > n - 1 - (m - 1); j--) swap(matrix[i][j], matrix[i + k][j]);
} bool Check(int n) { int sum = (n*(n*n + 1)) / 2; int SumA = 0, SumB = 0;
for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) SumA += matrix[i][j]; if (SumA != sum) return false; SumA = 0; }
for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) SumA += matrix[j][i]; if (SumA != sum) return false; SumA = 0; }
for (int i = 0; i<n; i++) { SumA += matrix[i][i]; SumB += matrix[i][n - i - 1]; } if (SumA != sum || SumB != sum) return false;
return true;
}
int main() { int n; cin >> n; if (n % 2 != 0) CreateOddMagicSquare(n); else if (n % 4 == 0) CreateDoubleEvenMagicSqure(n); else if (n % 2 == 0) CreateSingleEvenMagicSqure(n);
for (int i = 0; i<n; i++) { for (int j = 0; j<n; j++) cout << matrix[i][j] << "t"; cout << endl; }
if (!Check(n)) cout << "Failed to generate!" << endl; getchar(), getchar(); return 0; }
|