标签:style blog color strong os 数据
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
----------------------------------------------------------------------------------------------------------------------------------------
思路:
------------------------------------------------------------------------------------------------------------------------------------------
代码:
1 #include<stdio.h> 2 int main(int argc, char const *argv[]) 3 { 4 int i,j,m,n,num,flag; 5 int arr1[10][10],arr2[10][10]; 6 while(scanf("%d",&m)!=EOF) 7 { 8 if (m==0) 9 break; 10 scanf("%d",&n); 11 for (i = 0; i < m; ++i) 12 for(j=0;j<n;++j) 13 scanf("%d",&arr1[i][j]); 14 for (i = 0; i < m; i++) 15 for(j=0;j<n;++j) 16 scanf("%d",&arr2[i][j]); 17 num=0; 18 for(i=0;i<m;i++) 19 for(j=0;j<n;j++) 20 arr1[i][j]=arr1[i][j]+arr2[i][j]; 21 for(i=0;i<m;i++) 22 { 23 flag=1; 24 for(j=0;j<n;j++) 25 { 26 if(arr1[i][j]!=0) 27 { 28 flag=0; 29 break; 30 } 31 } 32 if(flag) 33 num++; 34 } 35 for(j=0;j<n;j++) 36 { 37 flag=1; 38 for(i=0;i<m;i++) 39 { 40 if(arr1[i][j]!=0) 41 { 42 flag=0; 43 break; 44 } 45 } 46 if(flag) 47 num++; 48 49 } 50 printf("%d\n",num); 51 } 52 return 0; 53 }
题目1001:A+B for Matrices,布布扣,bubuko.com
标签:style blog color strong os 数据
原文地址:http://www.cnblogs.com/qq1029579233/p/3853443.html