```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 #include #include #include using namespace std; #define MAX 100 void XayDungMaPhuongLe(vector > &matrix, int n); void XayDungMaPhuongChanKep(vector > &matrix, int n); void XayDungMaPhuongChanDonLe(vector > &matrix, int n); void XayDungMaPhuong(vector > &matrix, int n); void XuatMaTran(vector > &matrix, int n); void XuatMaTran(vector > &matrix, int n) { for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { printf("%4d", matrix[i][j]); } printf("\n\n"); } } void XayDungMaPhuongLe(vector > &matrix, int n) { int TongSoPhanTu = n * n; int i = 0, j = n / 2; // vị trí bắt đầu for(int k = 1; k <= TongSoPhanTu; k++) { matrix[i][j] = k; i--; j++; if(k % n == 0) { i += 2; --j; } else { if(j == n) { j -= n; } else if(i < 0) { i += n; } } } } /* Ma phương chẵn có 2 loại: - Ma phương kép: dạng 4n(n >= 1) VD: 4, 8, 12, 16 - Ma phương đơn lẻ: dạng 4n + 2 (n >= 1) VD: 6, 10, 14, 18 */ void XayDungMaPhuongChanKep(vector > &matrix, int n) { vector > I(n, vector (n, 0)); vector > J(n, vector (n, 0)); int i, j; // Xây dựng I, J int index = 1; for(i = 0; i < n; i++) for(j = 0; j < n; j++) { I[i][j] = ((i + 1) % 4) / 2; J[j][i] = ((i + 1) % 4) / 2; matrix[i][j] = index; index++; } for(i = 0; i < n; i++) for(j = 0; j < n; j++) { if(I[i][j] == J[i][j]) { matrix[i][j] = n * n + 1 - matrix[i][j]; } } } void XayDungMaPhuongChanDonLe(vector > &matrix, int n) { int p = n / 2; vector > M(p, vector (p, 0)); XayDungMaPhuong(M, p); int i, j, k; for (i = 0; i < p; i++) for (j = 0; j < p; j++) { matrix[i][j] = M[i][j]; matrix[i + p][j] = M[i][j] + 3 * p * p; matrix[i][j + p] = M[i][j] + 2 * p * p; matrix[i + p][j + p] = M[i][j] + p * p; } if (n == 2) return; vector I(p, 0); vector J; for (i = 0; i < p; i++) I[i] = i + 1; k = (n - 2) / 4; for (i = 1; i <= k; i++) J.push_back(i); for (i = n - k + 2; i <= n; i++) J.push_back(i); int temp; for (i = 1; i <= p; i++) for (j = 1; j <= J.size(); j++) { temp = matrix[i - 1][J[j - 1] - 1]; matrix[i - 1][J[j - 1] - 1] = matrix[i + p - 1][J[j - 1] -1]; matrix[i + p - 1][J[j - 1] - 1] = temp; } //j = 1, i //i = k + 1, k + 1 + p i = k; j = 0; temp = matrix[i][j]; matrix[i][j] = matrix[i + p][j]; matrix[i + p][j] = temp; j = i; temp = matrix[i + p][j]; matrix[i + p][j] = matrix[i][j]; matrix[i][j] = temp; } void XayDungMaPhuong(vector > &matrix,int n) { if (n % 2 == 1) //n Chẵn XayDungMaPhuongLe(matrix, n); else //n lẻ if (n % 4 == 0) // Chẵn kép XayDungMaPhuongChanKep(matrix, n); else // Chẵn đơn XayDungMaPhuongChanDonLe(matrix, n); } int main(int argc, char* argv[]) { int n; printf("\nNhap n: "); scanf("%d", &n); vector > matrix(n, vector (n, 0)); if (n < 3) { printf("\nLoi!n phai lon hon 2 \n\n"); return -1; } XayDungMaPhuong(matrix, n); XuatMaTran(matrix, n); getch(); return 0; } ```
 ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ``` ```Line 17: error: conio.h: No such file or directory Line 16: error: vector: No such file or directory Line 5: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'namespace' Line 9: error: expected ')' before '<' token Line 10: error: expected ')' before '<' token Line 11: error: expected ')' before '<' token Line 12: error: expected ')' before '<' token Line 13: error: expected ')' before '<' token Line 15: error: expected ')' before '<' token Line 27: error: expected ')' before '<' token Line 64: error: expected ')' before '<' token Line 90: error: expected ')' before '<' token Line 143: error: expected ')' before '<' token In function 'main': Line 158: error: 'vector' undeclared (first use in this function) Line 158: error: (Each undeclared identifier is reported only once Line 158: error: for each function it appears in.) Line 158: error: expected expression before 'int' Line 165: error: 'matrix' undeclared (first use in this function) ```