输入格式:
第1行是两个整数m、n。输入保证1 <= m <= 100, 1 <= n <= 200
接下来输入m行,每行n个整数。输入保证在32位整型取值范围之内
输出格式:
如果输入的每一行偶数的个数是偶数,
并且每一列奇数的个数是奇数,输出yes;否则输出no
思路:
输入两个整数m、n
嵌套循环输入m行,每行n个整数
统计每行偶数个数
统计每列奇数个数
设两个一维数组存储偶数奇数个数
嵌套循环遍历二维数组
统计个数
输入样例1:
3 5
1 7 8 9 6
2 7 5 0 7
4 3 8 2 6
输出样例1:
yes
输入样例2:
3 4
1 8 8 9
2 7 5 0
4 3 6 2
输出样例2:
no
输出样例2提示:
输入的第2列
8
7
3
奇数的个数不是奇数,所以输出 no
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
int n = scanner.nextInt();
int[][] a = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = scanner.nextInt();
}
}
int[] rowCount = new int[m];
int[] colCount = new int[n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j] % 2 == 0) {
rowCount[i]++;
}
if (a[i][j] % 2 != 0) {
colCount[j]++;
}
}
}
boolean isValid = true;
for (int i = 0; i < m; i++) {
if (rowCount[i] % 2 != 0) {
isValid = false;
break;
}
}
for (int j = 0; j < n; j++) {
if (colCount[j] % 2 == 0) {
isValid = false;
break;
}
}
if (isValid) {
System.out.println("yes");
} else {
System.out.println("no");
}
scanner.close();
}
}