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
#include<iostream> using namespace std; struct Matrix { int matrix[10][10]; int row,col; Matrix(int r,int c):row(r),col(c){} }; Matrix sum(Matrix x,Matrix y) { Matrix answer(x.row,x.col); for(int i=0;i<answer.row;i++) { for(int j=0;j<answer.col;j++) { answer.matrix[i][j]=x.matrix[i][j]+y.matrix[i][j]; } } return answer; } int judge(Matrix x) { int num=0; for(int i=0;i<x.row;i++)//判断横行 { int flag=1; for(int j=0;j<x.col;j++) { if(x.matrix[i][j]!=0) { flag=0; break; } } if(flag){ num++; } } for(int j=0;j<x.col;j++)//判断列 { int flag=1; for(int i=0;i<x.row;i++) { if(x.matrix[i][j]!=0) { flag=0; break; } } if(flag){ num++; } } return num; } int main() { int m,n; while(cin>>m>>n) { if(m==0) { break; } Matrix x(m,n); Matrix y(m,n); for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { cin>>x.matrix[i][j]; } } for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { cin>>y.matrix[i][j]; } } Matrix answer=sum(x,y); cout<<judge(answer)<<endl; } return 0; }
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); } }
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); } } }
#include <iostream> using namespace std; int main() { int m = 0, n = 0, matrice[10][10], ans = 0; while (cin >> m && 0 != m && cin >> n) { int rows[10] = { 0 }, cols[10] = { 0 }; for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) cin >> matrice[i][j]; for (int i = 0; i < m; ++i) for (int j = 0, tmp = 0; j < n; ++j){cin >> tmp; matrice[i][j] += tmp; if (matrice[i][j]) rows[i] = cols[j] = 1;} ans = m + n; while (m--) ans -= rows[m]; while (n--) ans -= cols[n]; cout << ans << endl; } return 0; }
#include <stdio.h> #include <stdint.h> /* 矩阵加法 分清楚行列计数即可 */ typedef struct { int matrix[11][11]; int row, col; } Matrix; Matrix NewMatrix(int row, int col) { Matrix rev; rev.row = row; rev.col = col; return rev; } void PrintMatrix(Matrix x) { for (int i = 0; i < x.row; i++) { for (int j = 0; j < x.col; j++) printf("%d ", x.matrix[i][j]); printf("\n"); } } Matrix Add(Matrix x, Matrix y) { Matrix ans = NewMatrix(x.row, x.col); for (int i = 0; i < x.row; i++) for (int j = 0; j < x.col; j++) ans.matrix[i][j] = x.matrix[i][j] + y.matrix[i][j]; return ans; } int GetNum(Matrix x) { int count = 0; for (int i = 0; i < x.row; i++) { // 先遍历行 int flag = 1; for(int j=0;j<x.col;j++) { if(x.matrix[i][j] != 0) { flag = 0; break; } } if(flag == 1) count++; } for (int i = 0; i < x.col; i++) { // 遍历列 int flag = 1; for(int j=0;j<x.row;j++) { if(x.matrix[j][i] != 0) { flag = 0; break; } } if(flag == 1) count++; } return count; } int main() { freopen("data.txt", "r", stdin); int m, n; while (scanf("%d%d", &m, &n) != EOF) { if (m == 0) break; Matrix x = NewMatrix(m, n); Matrix y = NewMatrix(m, n); for (int i = 0; i < x.row; i++) for (int j = 0; j < x.col; j++) scanf("%d", &x.matrix[i][j]); for (int i = 0; i < y.row; i++) for (int j = 0; j < y.col; j++) scanf("%d", &y.matrix[i][j]); Matrix ans = Add(x, y); int rev = GetNum(ans); printf("%d\n", rev); } return 0; }
#include <iostream> using namespace std; int main(){ int matrixA[11][11]; int matrixB[11][11]; int matrixC[11][11]; int n,m; while(cin>>n>>m){ for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ cin>>matrixA[i][j]; } } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ cin>>matrixB[i][j]; } } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ matrixC[i][j]=matrixA[i][j]+matrixB[i][j]; } } int count=0; for(int i=0;i<n;i++){ int flag=0; for(int j=0;j<m;j++){ if(matrixC[i][j]!=0){ flag=1; } } if(flag==0) count++; } for(int i=0;i<m;i++){ int flag=0; for(int j=0;j<n;j++){ if(matrixC[j][i]!=0){ flag=1; } } if(flag==0) count++; } cout<<count<<endl; } return 0; }
#include<iostream> #include<vector> using namespace std; vector< vector<int> > add(vector< vector<int> > x,vector< vector<int> > y){ int row = x.size(); int col = x[0].size(); vector< vector<int> > ans(row,vector<int>(col)); for(int i=0; i<row; i++){ for(int j=0;j<col;j++){ ans[i][j]=x[i][j]+y[i][j]; } } return ans; } int main() { int m, n; while (cin >> m && m != 0) { cin >> n; vector< vector<int> > a(m, vector<int>(n)); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { int temp; cin >> temp; a[i][j] = temp; } } vector< vector<int> > b(m, vector<int>(n)); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { int temp; cin >> temp; b[i][j] = temp; } } vector< vector<int> > result = add(a, b); //遍历行 int count_row=0; for (int i = 0; i < m; i++) { int flag = 1; for (int j = 0; j < n; j++) { if(result[i][j]!=0){ flag = 0; break; } } if(flag==1) count_row++; } //遍历列 int count_col=0; for(int j=0;j<n;j++){ int flag=1; for(int i=0;i<m;i++){ if(result[i][j]!=0){ flag = 0; break; } } if(flag==1) count_col++; } cout<<count_row+count_col<<endl; } return 0; }
#include <array> #include <iostream> #include <istream> #include <ostream> using namespace std; const int MAX_SIZE = 10; struct Matrix { array<array<int, MAX_SIZE>, MAX_SIZE>matrix; int rows, cols; //行数和列数 Matrix(int r, int c): rows(r), cols(c) {} //构造函数 //重载输入流运算符 friend istream& operator>>(istream& in, Matrix& m); //重载加法运算符 Matrix operator+(const Matrix& m); }; istream& operator>>(istream& in, Matrix& m) { for (int i = 0; i < m.rows; i++) { for (int j = 0; j < m.cols; j++) { in >> m.matrix[i][j]; } } return in; } Matrix Matrix::operator+(const Matrix& m) { Matrix result(rows, cols); for (int i = 0; i < m.rows; i++) { for (int j = 0; j < m.cols; j++) { result.matrix[i][j] = matrix[i][j] + m.matrix[i][j]; } } return result; } int main() { int m, n; while (cin >> m && m != 0) { cin >> n; Matrix A(m, n), B(m, n); cin >> A >> B; Matrix C = A + B; int count = 0; //全为0的行和列总数 //统计全为0的行数 for (int i = 0; i < C.rows; i++) { int count0 = 0; //统计一行0的个数 for (int j = 0; j < C.cols; j++) { if (C.matrix[i][j] == 0) { count0++; } } if (count0 == C.cols) { count++; } } //统计全为0的列数 for (int j = 0; j < C.cols; j++) { int count0 = 0; //统计一列0的个数 for (int i = 0; i < C.rows; i++) { if (C.matrix[i][j] == 0) { count0++; } } if (count0 == C.rows) { count++; } } cout << count << endl; } return 0; }
#include <endian.h> #include <iostream> #include <cmath> using namespace std; struct matrix { int M[10][10]; int row, col; matrix(int row, int col) :row(row), col(col) {} }; //矩阵加法 matrix Add(matrix A, matrix B, int row, int col) { matrix S(row, col); for (int i = 0; i < row; i++) for (int j = 0; j < col; j++) S.M[i][j] = A.M[i][j] + B.M[i][j]; return S; } int Count_zero(matrix S, int row, int col) { int count = 0; for (int i = 0; i < row; i++) { int flag = 0; for (int j = 0; j < col; j++) if (S.M[i][j] != 0) flag = 1; if (flag == 0) count++; } for (int j = 0; j < col; j++) { int flag = 0; for (int i = 0; i < row; i++) if (S.M[i][j] != 0) flag = 1; if (flag == 0) count++; } return count; } int main() { int row, col; while (cin >> row >> col && row && col) { matrix A(row, col); for (int i = 0; i < row; i++) for (int j = 0; j < col; j++) cin >> A.M[i][j]; matrix B(row, col); for (int i = 0; i < row; i++) for (int j = 0; j < col; j++) cin >> B.M[i][j]; matrix S = Add(A, B, row, col); cout << Count_zero(S, row, col); } }
#include <stdio.h> int main() { int row, col, sum; int a[10][10], b[10][10]; while (scanf("%d %d", &row, &col) != EOF && row!=0) { sum = 0; for (int i = 0; i < row; i ++) { for (int j = 0; j < col; j ++) { scanf("%d", &a[i][j]); } } for (int i = 0; i < row; i ++) { for (int j = 0; j < col; j ++) { scanf("%d", &b[i][j]); } } for (int i = 0; i < row; i ++) { for (int j = 0; j < col; j ++) { a[i][j] += b[i][j]; } } for (int i = 0; i < row; i ++) { int flag = 1; for (int j = 0; j < col; j ++) { if (a[i][j] != 0) { flag = 0; break; } } if (flag == 1) { sum ++; } } for (int i = 0; i < col; i ++) { int flag = 1; for (int j = 0; j < row; j ++) { if (a[j][i] != 0) { flag = 0; break; } } if (flag == 1) { sum ++; } } printf("%d\n", sum); } return 0; }
#include <iostream> using namespace std; struct Matrix { int matrix[10][10]; int row; int col; Matrix(int r, int c): row(r), col(c) {} }; void Print(Matrix x) { for (int i = 0; i < x.row; ++i) { for (int j = 0; j < x.col; ++j) { cout << x.matrix[i][j] << ' '; } cout << endl; } } int main() { int m, n; while (cin >> m >> n) { if (m == 0) break; Matrix a(m, n); Matrix b(m, n); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { cin >> a.matrix[i][j]; } } for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { cin >> b.matrix[i][j]; } } //add for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { a.matrix[i][j] += b.matrix[i][j]; } } int answer = 0; bool flag=true; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (a.matrix[i][j] != 0) { flag=false; break; } } if(flag){ answer++; } flag=true; } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (a.matrix[j][i] != 0) { flag = false; break; } } if(flag){ answer++; } flag=true; } cout << answer << endl; } } // 64 位输出请用 printf("%lld")
#include <iostream> #include <cstdio> using namespace std; const int MAXN=100; struct Matrix{ int row; int col; int matrix[MAXN][MAXN]; Matrix(int r,int c):row(r),col(c){} }; Matrix Add(Matrix x,Matrix y){ Matrix answer(x.row,x.col); for(int i=0;i<answer.row;++i){ for(int j=0;j<answer.col;++j){ answer.matrix[i][j]=x.matrix[i][j]+y.matrix[i][j]; } } return answer; } void InPut(Matrix& x){ for(int i=0;i<x.row;++i){ for(int j=0;j<x.col;++j){ scanf("%d",&x.matrix[i][j]); } } return; } int Judge(Matrix x){ int answer=0; for(int i=0;i<x.row;++i){ bool flag=true; for(int j=0;j<x.col;++j){ if(x.matrix[i][j]!=0){ flag=false; continue; } } if(flag){ answer++; } } for(int i=0;i<x.col;++i){ bool flag=true; for(int j=0;j<x.row;++j){ if(x.matrix[j][i]!=0){ flag=false; continue; } } if(flag){ answer++; } } return answer; } int main(){ int m,n; while(scanf("%d%d",&m,&n)!=EOF){ if(m==0){ break; } Matrix x(m,n); Matrix y(m,n); Matrix result(m,n); InPut(x); InPut(y); result=Add(x,y); printf("%d\n",Judge(result)); } return 0; }
#include<iostream> using namespace std; int main() { int M,N; int A[10][10]; int B[10][10]; while(cin>>M) { if(M==0) break; cin>>N; for(int i=0;i<M;i++) for(int j=0;j<N;j++) scanf("%d",&(A[i][j])); for(int i=0;i<M;i++) for(int j=0;j<N;j++) scanf("%d",&(B[i][j])); for(int i=0;i<M;i++) for(int j=0;j<N;j++) A[i][j]+=B[i][j]; int res=0; for(int i=0;i<M;i++) for(int j=0;j<N;j++) { if(A[i][j]!=0) break; if(j==N-1&&A[i][j]==0) res++; } for(int i=0;i<N;i++) for(int j=0;j<M;j++) { if(A[j][i]!=0) break; if(j==M-1&&A[j][i]==0) res++; } cout<<res<<endl; } }
//其实这题算简单吧,第一步输入两个矩阵,然后建立相加的模型,最后再判断,输出结果 #include <cstdio> #include <iostream> #include <cmath> using namespace std; //建立结构体 struct zhen{ int zhens[10][10]; int row,col; zhen(int r,int c):row(r),col(c){}; }; zhen toall(zhen x,zhen y){ zhen ans(x.row,x.col); for(int i=0;i<ans.row;i++){ for(int j=0;j<ans.col;j++){ ans.zhens[i][j]=x.zhens[i][j]+y.zhens[i][j]; } } return ans; } int iszore(zhen z){ int count,sum=0; for(int i=0;i<z.row;i++){ count=0; for(int j=0;j<z.col;j++){ if(z.zhens[i][j]==0){ count++; } } if(count==z.col){ sum++; } } for(int i=0;i<z.col;i++){ count=0; for(int j=0;j<z.row;j++){ if(z.zhens[j][i]==0){ count++; } } if(count==z.row){ sum++; } } return sum; } int main(){ int m,n; while(scanf("%d",&m)!=EOF){ if(m==0){ break; } scanf("%d",&n); zhen x(m,n); zhen y(m,n); for(int i=0;i<x.row;i++){ for(int j=0;j<x.col;j++){ scanf("%d",&x.zhens[i][j]); } } for(int i=0;i<y.row;i++){ for(int j=0;j<y.col;j++){ scanf("%d",&y.zhens[i][j]); } } zhen z=toall(x,y); int num=iszore(z); printf("%d\n",num); } return 0; }
//其实这题算简单吧,第一步输入两个矩阵,然后建立相加的模型,最后再判断,输出结果 #include <stdio.h> #include <stdlib.h> //建立结构体 typedef struct zhen{ int zhens[10][10]; int row,col; //zhen(int r,int c):row(r),col(c){}; }zhen; zhen toall(zhen x,zhen y){ zhen ans; ans.row=x.row; ans.col=x.col; for(int i=0;i<ans.row;i++){ for(int j=0;j<ans.col;j++){ ans.zhens[i][j]=x.zhens[i][j]+y.zhens[i][j]; } } return ans; } int iszore(zhen z){ int count,sum=0; for(int i=0;i<z.row;i++){ count=0; for(int j=0;j<z.col;j++){ if(z.zhens[i][j]==0){ count++; } } if(count==z.col){ sum++; } } for(int i=0;i<z.col;i++){ count=0; for(int j=0;j<z.row;j++){ if(z.zhens[j][i]==0){ count++; } } if(count==z.row){ sum++; } } return sum; } int main(){ int m,n; while(scanf("%d",&m)!=EOF){ if(m==0){ break; } scanf("%d",&n); zhen x; x.row=m; x.col=n; zhen y; y.row=m; y.col=n; for(int i=0;i<x.row;i++){ for(int j=0;j<x.col;j++){ scanf("%d",&x.zhens[i][j]); } } for(int i=0;i<y.row;i++){ for(int j=0;j<y.col;j++){ scanf("%d",&y.zhens[i][j]); } } zhen z=toall(x,y); int num=iszore(z); printf("%d\n",num); } return 0; }
#include<iostream> using namespace std; const int N = 10 + 10; struct Matrix{ int row, col; int matrix[N][N] = {0}; Matrix(){} Matrix(int r, int c): row(r), col(c){} }; Matrix Add(Matrix x, Matrix y){ Matrix ans = Matrix(x.row, x.col); for(int i = 0;i < ans.row;i++){ for(int j = 0;j < ans.col;j++){ ans.matrix[i][j] = x.matrix[i][j] + y.matrix[i][j]; } } return ans; } void input(Matrix &x){ for(int i = 0;i < x.row;i++){ for(int j = 0;j < x.col;j++){ scanf("%d", &x.matrix[i][j]); } } } int main(){ int m, n; while(scanf("%d", &m) != EOF){ if(m == 0) break; scanf("%d", &n); Matrix x(m, n); Matrix y(m, n); input(x); input(y); Matrix ans = Add(x, y); int count = 0; for(int i = 0;i < ans.row;i++){ bool flag = true; for(int j = 0;j < ans.col;j++){ if(ans.matrix[i][j] != 0){ flag = false; break; } } if(flag) count++; } for(int i = 0;i < ans.col;i++){ bool flag = true; for(int j = 0;j < ans.row;j++){ if(ans.matrix[j][i] != 0){ flag = false; break; } } if(flag) count++; } printf("%d\n", count); } return 0; }
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn = 110; //给出两个同型矩阵 A,B(行列数在每组数据第一行给出),判断矩阵 A+B 全 0 行和全 0 列共有多少 int main() { int m, n; int martix[maxn][maxn]; while(cin >> m) { if(m == 0) break; cin >> n; // 初始化和输入 memset(martix, 0, sizeof(martix)); for(int i = 0; i < m; ++i) { for(int j = 0; j < n; ++j) cin >> martix[i][j]; } for(int i = 0; i < m; ++i) { for(int j = 0; j < n; ++j) { int temp; cin >> temp; martix[i][j] += temp; } } // 判断 int temp, ans = 0; for(int i = 0; i < m; ++i) { bool flag = true; for(int j = 0; j < n; ++j) { if(martix[i][j] != 0) flag = false; } if(flag == true) ans++; } for(int i = 0; i < n; ++i) { bool flag = true; for(int j = 0; j < m; ++j) { if(martix[j][i] != 0) flag = false; } if(flag == true) ans++; } cout << ans << endl; } return 0; }
#include <cstdio> #include <iostream> using namespace std; typedef struct Matrix{ int matrix[10][10]; int row,col; Matrix(int r,int c):row(r),col(c){} }Matrix; int main(){ int m,n; while(scanf("%d%d",&m,&n)!=EOF&&m){ Matrix A(m,n); Matrix B(m,n); Matrix answer(m,n); int count=0; int flag = 1; for(int i=0;i<m;i++) for(int j=0;j<n;j++){ cin>>A.matrix[i][j]; } for(int i=0;i<m;i++) for(int j=0;j<n;j++){ cin>>B.matrix[i][j]; } for(int i=0;i<m;i++) for(int j=0;j<n;j++){ answer.matrix[i][j]=A.matrix[i][j]+B.matrix[i][j]; } for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ if(answer.matrix[i][j]!=0){ flag=0; break; } } if(flag==1)count++; else flag=1; } for(int j=0;j<n;j++){ for(int i=0;i<m;i++){ if(answer.matrix[i][j]!=0){ flag=0; break; } } if(flag==1)count++; else flag=1; } printf("%d\n",count); } return 0; }编译通过了但是有个疑问,一开始不判断EOF的时候一直死循环,,自己在本地测试以及自测输入都是可以输出正确结果正常退出的,想知道是测试样例有问题还是必须要判断EOF?