原题链接
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
| #include <bits/stdc++.h> using namespace std; const int N = 102; int M[N][N]; int n; int res[N]; int gauss() { int c = 0; for(int i = 1; i <= n; ++i) { bool change = false; for(int j = i; j <= n; ++j) { if(M[j][i] == 1) { for(int k = 1; k <= n + 1; ++k) swap(M[j][k], M[i][k]); c++; change = true; break; } } if(change) { for(int j = i + 1; j <= n; ++j) { if(M[j][i] == 1) { for(int k = i; k <= n + 1; ++k) { M[j][k] ^= M[i][k]; } } } } } if(c < n) { if(M[c+1][c+1] == 0 && M[c+1][n+1] == 0) return 1; else return 2; } for(int i = n; i >= 1; --i) { res[i] = M[i][n+1]; for(int j = i - 1; j >= 1; --j) { if(M[j][i] == 1) for(int k = i; k <= n + 1; ++k) { M[j][k] ^= M[i][k]; } } } return 0; }
int main() { scanf("%d", &n); for(int i = 1; i <= n; ++i) { for(int j = 1; j <= n + 1; ++j) { scanf("%d", &M[i][j]); } } int ret = gauss(); if(ret == 0) { for(int i = 1; i <= n; ++i) { printf("%d\n", res[i]); } } else if(ret == 1) { printf("Multiple sets of solutions"); } else { printf("No solution"); } return 0; }
|
注:有关多解无解的判断有问题,应该遍历所有没有选择的数值,查找是否有不为0的情况。如果不为0,说明无解。如果全为0,说明有无数多解,以后做请一定注意。