The process of this question is similar to Gaussian elimination. There are only two numbers, 1 and 0. The XOR operation is the same as 0, and the difference is 1. Any number and 0 XOR 0 ^ n = n, then it is simple, just like the linear equation solution
左下角消0
Enumerated column
Find the first non-zero line
exchange
Suppress zero (exclusive OR) of rows below the same column
判断3种情况
r=n, the only solution
r<n, the number of rows does not reach the last row, indicating that the last few rows are all 0
The last digit is not 0, no solution
The last digit is 0 infinitely many solutions
③. Learning points
④. Code implementation
import java.util.Scanner;publicclassMain{
staticint n;staticint a[][]=newint[110][110];publicstaticvoidmain(String[] args){
Scanner sc =newScanner(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]);}}elseif(t==1){
System.out.println("No solution");}else{
System.out.println("Multiple sets of solutions");}}staticintgauss(){
int c=0,r=0;for(; c <n; c++){
//在该列找出最大值1的行号int t=r;for(int i = r; i <n; i++){
if(a[i][c]!=0){
t=i;break;}}//当前列都是0就跳过if(a[t][c]==0){
continue;}//找到当前最大值的行 交换上面未固定的行//遍历每一列进行交换 r表示最顶上的那一行 t表示当前最大值的那一行for(int i =c; i <=n; i++){
int q=a[r][i];
a[r][i]=a[t][i];
a[t][i]=q;}//将第c列最大值下面的数全部变成0for(int i =r+1; i <n; i++){
if(a[i][c]!=0){
//将下面为1的列和当前最大值的列每一个数进行异或运算for(int j =c; j <=n; j++){
a[i][j]^=a[r][j];}}}
r++;}//行数没有到达最后一行 说明最后几行存在全为0if(r<n){
for(int i =r; i <n; i++){
if(a[i][n]!=0){
//最后一位不为0 无解return2;}}return1;}//唯一解从下往上推导 r=nfor(int i =r-1; i >=0; i--){
for(int j = i+1; j <n; j++){
//消除上面的1 使用&进行运算
a[i][n]^=a[i][j]&a[j][n];//只有1或0,所以*和&一样 }}return0;}}