The input consists of several test cases, each starts with a pair of positive integers M and N (≤10) which are the number of rows and columns of the matrices, respectively. Then 2*M lines follow, each contains N integers in [-100, 100], separated by a space. The first M lines correspond to the elements of A and the second M lines to that of B. The input is terminated by a zero M and that case must NOT be processed.
For each test case you should output in one line the total number of zero rows and columns of A+B.
2 2 1 1 1 1 -1 -1 10 9 2 3 1 2 3 4 5 6 -1 -2 -3 -4 -5 -6 0
1 5
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()){ int count=0; int M= scanner.nextInt(); if (M!=0){ int N =scanner.nextInt(); int[][] matrix = new int[M][N]; for (int i = 0; i < M; i++) for (int j = 0; j < N; j++) matrix[i][j]=scanner.nextInt(); for (int i = 0; i < M; i++) for (int j = 0; j < N; j++) matrix[i][j] += scanner.nextInt(); for (int i = 0; i < M; i++) { boolean flag=true; for (int j = 0; j < N; j++) { if (matrix[i][j]!=0) { flag=false; break; } } if (flag) count++; } for (int i = 0; i < N; i++) { boolean flag=true; for (int j = 0; j < M; j++) { if (matrix[j][i]!=0) { flag=false; break; } } if (flag) count++; } } System.out.println(count); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { int M, N; Scanner in = new Scanner(System.in); M = in.nextInt(); N = in.nextInt(); if (M == 0 || N == 0) return; int[][] A = new int[M][N]; // int[][] B = new int[M][N]; int[][] C = new int[M][N]; // 读取矩阵A、B for (int i = 0; i < M; i++) for (int j = 0; j < N; j++) A[i][j] = in.nextInt(); for (int i = 0; i < M; i++) for (int j = 0; j < N; j++) C[i][j] = A[i][j] + in.nextInt(); // 在输入B[i][j]的同时计算Aij + Bij int num = 0; // 计算零行和零列的总数 for (int i = 0; i < M; i++) { boolean flg = true; // 标志:是否全为零,true表示全为零 for (int j = 0; j < N; j++) { if (C[i][j] != 0) { flg = false; break; } } if (flg) num++; } for (int j = 0; j < N; j++) { boolean flg = true; // 标志:是否全为零,true表示全为零 for (int i = 0; i < M; i++) { if (C[i][j] != 0) { flg = false; break; } } if (flg) num++; } System.out.print(num); } }