九度题目1001:A+B for Matrices

来源:互联网 发布:linux软件开发平台 编辑:程序博客网 时间:2024/06/15 00:15

May 16, 2016
作者:dengshuai_super
出处:http://blog.csdn.net/dengshuai_super/article/details/51423783
声明:自由转载,转载请注明作者及出处。


题目1001:A+B for Matrices

题目描述:
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的行数和列数之和为ZeroNum,输出ZeroNum。

--        --        --        --       --        --|  1    1  |        | -1    -1 |       | 0      0 ||          |    +   |          |  =    |          ||  1    1  |        | 10     9 |       | 11    10 |--        --        --        --       --        --

整行为0的行数为1行+整列为0的列数为0列 = 1

--        --         --          --       --         --|  1  2  3  |        | -1  -2  -3 |       | 0   0   0 ||           |    +   |            |  =    |           ||  4  5  6  |        | -4  -5  -6 |       | 0   0   0 |--        --         --          --       --         --

整行为0的行数为2行+整列为0的列数为3列 = 5

代码如下:

/******************************************************************************   九度题目1001:A+B for Matrices*******************************************************************************   by Deng shuai 16 May 2016*   http://blog.csdn.net/dengshuai_super*******************************************************************************   Copyright (c) 2016, Deng Shuai*   All rights reserved.*****************************************************************************/#include <stdio.h>const int max = 100;int matrix[max][max];int m, n;int i, j;int main(int argc, char* argv[]){    while(scanf("%d", &m) != EOF){//EOF(End Of File)C语言中,EOF常被作为文件结束的标志。                                  //还有很多文件处理函数处错误后的返回值也是EOF,                                  //因此常被用来判断调用一个函数是否成功。在 UNIX中,                                   //EOF表示能从交互式 shell (终端) 送出 Ctrl+D (习惯性标准)。                                  //在微软的 DOS 与 Windows 中能送出 Ctrl+Z。        if(m == 0){            break;        }        scanf("%d", &n);        if(n == 0){            break;//当m,n为0时返回        }        for(i = 0 ; i < m; i++){            for(j = 0; j < n; j++){                scanf("%d", &matrix[i][j]);//按照从左到右,从上到下的顺序依次输入数字            }        }        for(i = 0 ; i < m; i++){            for(int j = 0; j < n; j++){                int tempNum;//保存第二个矩阵每个元素的临时变量                scanf("%d", &tempNum);                matrix[i][j] += tempNum;//直接把相加的结果放在matrix中            }        }        int ZeroNum = 0;        for (i = 0; i < m; i++) {//检查行            bool isZero = true;            for (j = 0; j < n; j++) {                if (matrix[i][j] != 0) {                    isZero = false;                    break;                }            }            if (isZero) {                ZeroNum++;            }        }        for (j = 0; j < n; j++) {//检查列            bool isZero = true;            for (int i = 0; i < m; i++) {                if (matrix[i][j] != 0) {                    isZero = false;                    break;                }            }            if (isZero) {                ZeroNum++;            }        }        printf("%d\n", ZeroNum);    }    return 0;}

截图:

这里写图片描述

0 0
原创粉丝点击