首页 > 试题广场 >

A+B for Matrices

[编程题]A+B for Matrices
  • 热度指数:5545 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
    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.
示例1

输入

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
#include <stdio.h>

int main() {
    int row, col, sum;
    int a[10][10], b[10][10];
    while (scanf("%d %d", &row, &col) != EOF && row!=0) {
        sum = 0;
        for (int i = 0; i < row; i ++) {
            for (int j = 0; j < col; j ++) {
                scanf("%d", &a[i][j]);
            }
        }
        for (int i = 0; i < row; i ++) {
            for (int j = 0; j < col; j ++) {
                scanf("%d", &b[i][j]);
            }
        }
        for (int i = 0; i < row; i ++) {
            for (int j = 0; j < col; j ++) {
                a[i][j] += b[i][j];
            }
        }
        for (int i = 0; i < row; i ++) {
            int flag = 1;
            for (int j = 0; j < col; j ++) {
                if (a[i][j] != 0) {
                    flag = 0;
                    break;
                }
            }
            if (flag == 1) {
                sum ++;
            }
        }
        for (int i = 0; i < col; i ++) {
            int flag = 1;
            for (int j = 0; j < row; j ++) {
                if (a[j][i] != 0) {
                    flag = 0;
                    break;
                }
            }
            if (flag == 1) {
                sum ++;
            }
        }
        printf("%d\n", sum);
    }

    return 0;
}

发表于 2023-02-10 14:43:54 回复(0)
//其实这题算简单吧,第一步输入两个矩阵,然后建立相加的模型,最后再判断,输出结果
#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
//建立结构体
struct zhen{
    int zhens[10][10];
    int row,col;
    zhen(int r,int c):row(r),col(c){};
};
zhen toall(zhen x,zhen y){
    zhen ans(x.row,x.col);
    for(int i=0;i<ans.row;i++){
            for(int j=0;j<ans.col;j++){
             ans.zhens[i][j]=x.zhens[i][j]+y.zhens[i][j];
        }
    }
    return ans;
}
int iszore(zhen z){
    int count,sum=0;
    for(int i=0;i<z.row;i++){
        count=0;
        for(int j=0;j<z.col;j++){
           if(z.zhens[i][j]==0){
               count++;
           }
        }
        if(count==z.col){
            sum++;
        }
    }
    for(int i=0;i<z.col;i++){
        count=0;
        for(int j=0;j<z.row;j++){
           if(z.zhens[j][i]==0){
               count++;
           }
        }
        if(count==z.row){
            sum++;
        }
    }
    
    return sum;
}
int main(){
    int m,n;
    while(scanf("%d",&m)!=EOF){
        if(m==0){
            break;
        }
        scanf("%d",&n);
        zhen x(m,n);
        zhen y(m,n);
        for(int i=0;i<x.row;i++){
            for(int j=0;j<x.col;j++){
                scanf("%d",&x.zhens[i][j]);
            }
        }
        for(int i=0;i<y.row;i++){
            for(int j=0;j<y.col;j++){
                scanf("%d",&y.zhens[i][j]);
            }
        }
        zhen z=toall(x,y);
        int num=iszore(z);
        printf("%d\n",num);
    }
    return 0;
}
c语言只需要些许修改:
//其实这题算简单吧,第一步输入两个矩阵,然后建立相加的模型,最后再判断,输出结果
#include <stdio.h>
#include <stdlib.h>


//建立结构体
typedef struct zhen{
    int zhens[10][10];
    int row,col;
    //zhen(int r,int c):row(r),col(c){};
}zhen;
zhen toall(zhen x,zhen y){
    zhen ans;
    ans.row=x.row;
    ans.col=x.col;
    for(int i=0;i<ans.row;i++){
            for(int j=0;j<ans.col;j++){
             ans.zhens[i][j]=x.zhens[i][j]+y.zhens[i][j];
        }
    }
    return ans;
}
int iszore(zhen z){
    int count,sum=0;
    for(int i=0;i<z.row;i++){
        count=0;
        for(int j=0;j<z.col;j++){
           if(z.zhens[i][j]==0){
               count++;
           }
        }
        if(count==z.col){
            sum++;
        }
    }
    for(int i=0;i<z.col;i++){
        count=0;
        for(int j=0;j<z.row;j++){
           if(z.zhens[j][i]==0){
               count++;
           }
        }
        if(count==z.row){
            sum++;
        }
    }
    
    return sum;
}
int main(){
    int m,n;
    while(scanf("%d",&m)!=EOF){
        if(m==0){
            break;
        }
        scanf("%d",&n);
        zhen x;
        x.row=m;
        x.col=n;
        zhen y;
        y.row=m;
        y.col=n;
        for(int i=0;i<x.row;i++){
            for(int j=0;j<x.col;j++){
                scanf("%d",&x.zhens[i][j]);
            }
        }
        for(int i=0;i<y.row;i++){
            for(int j=0;j<y.col;j++){
                scanf("%d",&y.zhens[i][j]);
            }
        }
        zhen z=toall(x,y);
        int num=iszore(z);
        printf("%d\n",num);
    }
    return 0;
}


发表于 2022-03-03 09:09:52 回复(0)

问题信息

难度:
2条回答 5472浏览

热门推荐

通过挑战的用户

查看代码