-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathExercise08_23.java
68 lines (60 loc) · 1.96 KB
/
Exercise08_23.java
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
package ch_08;
import java.util.*;
/**
* * 8.23 (Game: find the flipped cell) Suppose you are given a 6-by-6 matrix filled with
* 0s and 1s.
* <p>
* All rows and all columns have an even number of 1s. Let the user flip
* one cell (i.e., flip from 1 to 0 or from 0 to 1) and write a program to find which
* cell was flipped. Your program should prompt the user to enter a 6-by-6 array
* with 0s and 1s and find the first row r and first column c where the even number
* of the 1s property is violated (i.e., the number of 1s is not even). The flipped cell
* is at (r, c). Here is a sample run:
* 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)
*/
public class Exercise08_23 {
public static void main(String[] args) {
int[][] test = new int[6][6];
Scanner in = new Scanner(System.in);
System.out.println("Enter a 6 x 6 matrix row by row: ");
int invalidRow = -1; //Initialize
for (int i = 0; i < 6; i++) {
int rowCountOnes = 0;
for (int j = 0; j < 6; j++) {
int n = in.nextInt();
if (n == 1) {
rowCountOnes++;
}
test[i][j] = n;
}
if (rowCountOnes % 2 != 0) {
invalidRow = i;
}
}
int invalidCol = evalColumns(test);
System.out.println("The flipped cell is at (" + invalidRow + ", " + invalidCol + ")");
in.close();
}
static int evalColumns(int[][] array) {
int invalidColumn = -1;
for (int i = 0; i < 6; i++) {
int colCount = 0;
for (int j = 0; j < 6; j++) {
if (array[j][i] == 1) {
colCount++;
}
}
if (colCount % 2 != 0) {
invalidColumn = i;
}
}
return invalidColumn;
}
}