A+B for Matrices

版权声明:[email protected] https://blog.csdn.net/lytwy123/article/details/84493498

1.题目描述

This time, you are supposed to find A+B where A and B are two matrices, and then count the number of zero rows and columns.

输入
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, ea

输出
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


2.分析

这道题目好像是浙江大学研究生复试上机题的题目。
刚开始读题目,看不懂,很是尴尬,尝试过用百度翻译和有道翻译去翻译

这些个翻译真的水,根本没get到题目意思,这道题目大概意思是输入两个数M,N为矩阵的行,列,然后将A,B两个矩阵相加后,(相加后)的行列中的每一行为0则+1,每一列为0则加1.
举例子把
样例:2 3(输入2行三列的矩阵)
1 2 3
4 5 6 (这两行输入实际是输入矩阵A的)
-1 -2 -3
-4 -5 -6(这两行输入实际是输入矩阵B的)
2的M次方就是4,就是四行的意思,不是翻译说的那啥。
然后将A,B矩阵相加为
0 0 0
0 0 0
第一行,第二行的元素都为0,+2
第一列,第二列,第三列的元素都为0,+3
所以输出5.
题目意思是这个样子


3.源代码参考

#include <iostream>
using namespace std;

int A[10][10];
int B[10][10];
int AB[10][10]; //存储A矩阵+B矩阵的和的矩阵 

int main()
{
    int m,n;

    while(cin >> m && m != 0){
        cin >> n;
        int count = 0;//记录行、列为0的总数

        //输入矩阵A 
        for(int i = 0;i < m;i++){
            for(int j = 0;j < n;j++){
                cin >> A[i][j];
            }
        }
        //输入举证B,再进行相加的结果存到矩阵AB 
        for(int i = 0;i < m;i++){
            for(int j = 0;j < n;j++){
                cin >> B[i][j];
                AB[i][j] = A[i][j] + B[i][j];
            }
        }

        //这里我们判断每一行是否都为0,如果是flag为真,count++,否则不成立 
        for(int i = 0;i < m;i++){
            int flag = 1;
            for(int j = 0;j < n;j++){
                if(AB[i][j] != 0){
                    flag = 0;
                    break;
                }
            }
            if(flag){
                count++;
            }
        }

        //这里我们判断每一列是否都为0,如果是flag为真,count++,否则不成立
        for(int j = 0;j < n;j++){
            int flag = 1;
            for(int i = 0;i < m;i++){
                if(AB[i][j] != 0){
                    flag = 0;
                    break;
                }
            }
            if(flag){
                count++;
            }
        }
        cout << count << endl;
    }
    return 0;
}

如果英语没点书,还是恐怖啊浙大!

猜你喜欢

转载自blog.csdn.net/lytwy123/article/details/84493498
A+B
今日推荐