九度-1001-A+B for Matrices | Acm之家
http://blog.csdn.net/sjf0115/article/details/7686447
- 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.
int main( int argc, char **argv) |
10 | { |
11 | int i, j, m, n, nresult; |
12 |
13 | while (EOF != scanf ( "%d" , &m) && m) |
14 | { |
15 | memset (res, 0, sizeof (res)); |
16 | nresult = 0; |
17 | scanf ( "%d" , &n); |
18 | for (i = 0; i < m; ++i) |
19 | { |
20 | for (j = 0; j < n; ++j) |
21 | { |
22 | scanf ( "%d" , &matrix[0][i][j]); |
23 | } |
24 | } |
25 | for (i = 0; i < m; ++i) |
26 | { |
27 | for (j = 0; j < n; ++j) |
28 | { |
29 | scanf ( "%d" , &matrix[1][i][j]); |
30 | matrix[1][i][j] += matrix[0][i][j]; |
31 | if (matrix[1][i][j]) |
32 | { |
33 | res[i] = res[m + j] = 1; |
34 | } |
35 | } |
36 | } |
37 | for (i = 0; i < m + n; ++i) |
38 | { |
39 | if (!res[i]) |
40 | { |
41 | ++nresult; |
42 | } |
43 | } |
44 | printf ( "%d\n" , nresult); |
45 | } |
46 | return 0; |
47 | } |
- while(scanf("%d",&M),M){
- scanf("%d",&N);
- int i,j;
- for(i = 0;i<M;i++){
- for(j = 0;j<N;j++){
- scanf("%d",&Matrices[i][j]);
- }
- }
- int a;
- int count = 0;
- int CountOfZero = 0;
- for(i = 0;i<M;i++){
- for(j = 0;j<N;j++){
- scanf("%d",&a);
- Matrices[i][j] += a;
- count += Matrices[i][j];
- }
- if(count == 0){
- CountOfZero++;
- }
- count = 0;
- }
- //计算列
- count = 0;
- for(i = 0;i<N;i++){
- for(j = 0;j<M;j++){
- count += Matrices[j][i];
- }
- if(count == 0){
- CountOfZero++;
- }
- count = 0;
- }
- printf("%d\n",CountOfZero);
- }