- 题目描述:
-
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
----------------------------------------------------------------------------------------------------------------------------------------
思路:
- 结束以输入m==0为条件,所以可以先输入m,对起进行判断是否为0,为0则跳出循环
- 用二维数组进行存储行列数据,并且需要用到两个二维数组。将两个二维数组的值加入其中一个数组,对其每一行,每一列进行判断是否都为0,是的话则统计量num++
------------------------------------------------------------------------------------------------------------------------------------------
代码:
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
时间: 2024-12-19 20:24:26