原题链接
①. 题目
②. 思路
- 这题流程高斯消元差不多,里面只有1和0两个数,异或运算相同为0,不同为1,任何数与0异或 0 ^ n = n,那就简单了,和线性方程解一样
左下角消0
- 枚举列
- 找第一个非零行
- 交换
- 把同列下面行消零(异或)
判断3种情况
- r=n,唯一解
- r<n,行数没有到达最后一行 说明最后几行存在全为0
- 最后一位不为0 无解
- 最后一位为0 无穷多解
③. 学习点
④. 代码实现
import java.util.Scanner;
public class Main {
static int n;
static int a[][]=new int[110][110];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for (int i = 0; i <n; i++) {
for (int j = 0; j <=n; j++) {
a[i][j]=sc.nextInt();
}
}
int t=gauss();
if(t==0) {
for (int i = 0; i <n; i++) {
System.out.println(a[i][n]);
}
}else if(t==1) {
System.out.println("No solution");
}else {
System.out.println("Multiple sets of solutions");
}
}
static int gauss() {
int c=0,r=0;
for (; c <n; c++) {
int t=r;
for (int i = r; i <n; i++) {
if(a[i][c]!=0) {
t=i;
break;
}
}
if(a[t][c]==0) {
continue;
}
for (int i =c; i <=n; i++) {
int q=a[r][i];
a[r][i]=a[t][i];
a[t][i]=q;
}
for (int i =r+1; i <n; i++) {
if(a[i][c]!=0) {
for (int j =c; j <=n; j++) {
a[i][j]^=a[r][j];
}
}
}
r++;
}
if(r<n) {
for (int i =r; i <n; i++) {
if(a[i][n]!=0) {
return 2;
}
}
return 1;
}
for (int i =r-1; i >=0; i--) {
for (int j = i+1; j <n; j++) {
a[i][n]^=a[i][j]&a[j][n];
}
}
return 0;
}
}