1001 A+B for Matrices

來源:互聯網
上載者:User
題目描述:

    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, 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



統計下行列之和為0的即可

#include <iostream>using namespace std;int main(){int m,n;int a[11][11];int b[11][11];int c[11][11];int sum;int answer;while(cin>>m){sum=0;answer=0;if(m!=0){cin>>n;for(int i=0;i<m;i++)for(int j=0;j<n;j++){cin>>a[i][j];}for(int i=0;i<m;i++)for(int j=0;j<n;j++){cin>>b[i][j];}for(int i=0;i<m;i++)for(int j=0;j<n;j++){c[i][j]=a[i][j]+b[i][j];}           for(int i=0;i<m;i++)   {   sum=0;   for(int j=0;j<n;j++)   {   sum+=c[i][j];   }   if(sum==0)   {   answer++;   }   }   for(int i=0;i<n;i++)   {   sum=0;   for(int j=0;j<m;j++)   {   sum+=c[j][i];   }   if(sum==0)   {   answer++;   }   }   cout<<answer<<endl;}else{break;}}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.