# 第八章第二十三题（游戏：找到翻转的单元格）（Game: find flipped cells）

• *8.23（游戏：找到翻转的单元格）假设给定一个填满0和1的6×6矩阵，所有的行和列都有偶数个1。让用户翻转一个单元（即从1翻成0或者从0翻成1），编写一个程序，找到哪个单元格被翻转了。程序应该提示用户输入一个6x6的填满0和1的矩阵，并且找到一个不符合具有偶数个1的特征的r行以及c列（即1的数目不是偶数），则该翻转的单元格位于（r,c）。下面是一个运行示例：
Enter a 6-by-6 matrix row by row:
1 1 1 0 1 1
1 1 1 1 0 0
0 1 0 1 1 1
1 1 1 1 1 1
0 1 1 1 1 0
1 0 0 0 0 1
The flipped cell is at(0,1)
*8.23（Game: find flipped cells）Given a 6 × 6 matrix filled with 0 and 1, all rows and columns have even ones. Let the user flip a cell (from 1 to 0 or from 0 to 1), write a program to find which cell has been flipped. The program should prompt the user to enter a 6x6 matrix filled with 0 and 1, and find a row R and column C that do not meet the characteristic of even 1 (i.e. the number of 1 is not even), then the flipped cell is located in (R, c). Here is a running example:
Enter a 6-by-6 matrix row by row:
1 1 1 0 1 1
1 1 1 1 0 0
0 1 0 1 1 1
1 1 1 1 1 1
0 1 1 1 1 0
1 0 0 0 0 1
The flipped cell is at(0,1)

• 参考代码：

package chapter08;

import java.util.Scanner;

public class Code_23 {
public static void main(String[] args) {

Scanner input=new Scanner(System.in);
System.out.println("Enter a 6-by-6 matrix row by row:");
int[][] matrix =new int[6][6];

for(int i=0;i<matrix.length;i++){
for (int j=0;j<matrix[i].length;j++){
matrix[i][j]=input.nextInt();
}
}

System.out.println("The flipped cell is at("+findTheRow(matrix)+","+findTheColumn(matrix)+")");
}
public static int findTheRow(int[][] matrix){
int countRow=0;
int row=0;
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[i].length;j++){
if (matrix[i][j]==1)
countRow++;
}
if(countRow%2!=0){
row=i;
break;
}
}
return row;
}
public static int findTheColumn(int[][] matrix){
int countColumn=0;
int column=0;

for(int j=0;j<matrix[0].length;j++) {
for (int i = 0; i < matrix.length; i++) {
if (matrix[i][j] == 1)
countColumn++;
}
if (countColumn % 2 != 0) {
column = j;
break;
}
}

return column;
}
}


• 结果显示：

Enter a 6-by-6 matrix row by row:
1 1 1 0 1 1
1 1 1 1 0 0
0 1 0 1 1 1
1 1 1 1 1 1
0 1 1 1 1 0
1 0 0 0 0 1
The flipped cell is at(0,1)

Process finished with exit code 0



07-17 3694
08-03 977

07-06 474
08-03 20
07-31 1367
01-12 1490
01-30 428
05-26 6943
07-17 2568
05-28 49
12-08 5万+