题解 | #A+B for Matrices#
A+B for Matrices
https://www.nowcoder.com/practice/e431b3ae9efa4726b45a659b71abe124
#include <iostream> #include <cstring> #include <algorithm> #include <math.h> #include <stdlib.h> using namespace std; #define M 10 #define N 10 /* 题意: 输入:第一行输入矩阵的行列数;然后按顺序输入A、B的元素值; 输出:A+B之后,可能会有等于0的行或列,输出等于0的 行+列的总数 */ int main(){ int m,n; while(cin >>m>>n && m != 0 && n != 0){ int A[M][N],B[M][N]; //输入A for (int i = 0; i < m; i++){ for (int j = 0; j < n; j++){ cin >>A[i][j]; } } //输入B for (int i = 0; i < m; i++){ for (int j = 0; j < n; j++){ cin >>B[i][j]; } } //A+B for (int i = 0; i < m; i++){ for (int j = 0; j < n; j++){ A[i][j] +=B[i][j]; } } int count = 0; //计算等于0的行数 for (int i = 0; i < m; i++){ int flag = 1; for (int j = 0; j < n; j++){ if (A[i][j] != 0) flag = 0; } if (flag) count++; } //计算等于0的列数 for (int i = 0; i < n; i++){ int flag = 1; for (int j = 0; j < m; j++){ if (A[j][i] != 0) flag = 0; } if (flag) count++; } cout<<count<<endl; } }