首页 > 试题广场 >

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
Java 解法
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int count=0;
            int M= scanner.nextInt();
            if (M!=0){
                int N =scanner.nextInt();
                int[][] matrix = new int[M][N];
                for (int i = 0; i < M; i++) 
                    for (int j = 0; j < N; j++)
                        matrix[i][j]=scanner.nextInt();
                for (int i = 0; i < M; i++)
                    for (int j = 0; j < N; j++) 
                        matrix[i][j] += scanner.nextInt();
                    
                for (int i = 0; i < M; i++) {
                    boolean flag=true;
                    for (int j = 0; j < N; j++) {
                        if (matrix[i][j]!=0) {
                            flag=false;
                            break;
                        }
                    }
                    if (flag) count++;
                }

                for (int i = 0; i < N; i++) {
                    boolean flag=true;
                    for (int j = 0; j < M; j++) {
                        if (matrix[j][i]!=0) {
                            flag=false;
                            break;
                        }
                    }
                    if (flag) count++;
                }
            }
            System.out.println(count);    
        }
    }
}


发表于 2020-03-06 21:59:05 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int M, N;
        Scanner in = new Scanner(System.in);
        M = in.nextInt();
        N = in.nextInt();
        if (M == 0 || N == 0)
            return;
        int[][] A = new int[M][N];
//        int[][] B = new int[M][N];
        int[][] C = new int[M][N];
        // 读取矩阵A、B
        for (int i = 0; i < M; i++)
            for (int j = 0; j < N; j++)
                A[i][j] = in.nextInt();
        for (int i = 0; i < M; i++)
            for (int j = 0; j < N; j++)
                C[i][j] = A[i][j] + in.nextInt(); // 在输入B[i][j]的同时计算Aij + Bij
        int num = 0;
        // 计算零行和零列的总数
        for (int i = 0; i < M; i++) {
            boolean flg = true; // 标志:是否全为零,true表示全为零
            for (int j = 0; j < N; j++) {
                if (C[i][j] != 0) {
                    flg = false;
                    break;
                }
            }
            if (flg)
                num++;
        }
        for (int j = 0; j < N; j++) {
            boolean flg = true; // 标志:是否全为零,true表示全为零
            for (int i = 0; i < M; i++) {
                if (C[i][j] != 0) {
                    flg = false;
                    break;
                }
            }
            if (flg)
                num++;
        }
        System.out.print(num);
    }
}
编辑于 2018-03-18 13:29:56 回复(0)

问题信息

难度:
2条回答 5474浏览

热门推荐

通过挑战的用户

查看代码