A+B for Matrices

来源:互联网 发布:2017年10月份宏观数据 编辑:程序博客网 时间:2024/05/16 10:45
题目描述:    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 <stdio.h>#include<string.h>#define M 10int main(){int matrix[M][M], matrix_2[M][M];int record[M + M];int m, n;while (EOF != scanf("%d %d", &m, &n) && m){int num = 0;memset(record, 0, sizeof(record));for (int i = 0; i < m; i++){for (int j = 0; j < n; j++){scanf("%d", &matrix[i][j]);}}for (int i = 0; i < m; i++){for (int j = 0; j < n; j++){scanf("%d", &matrix_2[i][j]);matrix[i][j] += matrix_2[i][j];if (matrix[i][j]){record[i] = 1;//全为0的行的记录record[m + j] = 1;  //全为0的列的记录 }}}for (int i = 0; i < m + n; i++){if (!record[i]){num++;}}printf("%d\n", num);}return 0;}


0 0
原创粉丝点击