L1-048 matrix A multiplied by B AC code (JAVA)

topic

Given two matrices A and B, you are required to calculate their product matrix AB. It should be noted that only matrices with matching scales can be multiplied. That is, if A has R​a rows and
Ca columns, and B has R​b​​rows and C​b columns, then the two matrices can be multiplied only when Ca and Rb are equal.

Input format:
Input two matrices A and B successively. For each matrix, first give the number of rows R and the number of columns C in a row, and then R rows, each row gives C integers, separated by 1 space, and there is no extra space at the beginning and end of the row. Enter to ensure that the R and C of the two matrices are positive numbers, and the absolute value of all integers does not exceed 100.

Output format:
If the scales of the two input matrices match, output the product matrix AB according to the input format, otherwise output Error: Ca != Rb, where Ca is the number of columns in A and Rb is the number of rows in B.

Input example 1:
2 3
1 2 3
4 5 6
3 4
7 8 9 0
-1 -2 -3 -4
5 6 7 8
Output example 1:
2 4
20 22 24 16
53 58 63 28
Input example 2 :
3 2
38 26
43 -5
0 17
3 2
-11 57
99 68
81 72
Output sample 2:
Error: 2 != 3

Note : Use Scanner to test point 3 will time out error, you need to use BufferedReader stream to read data
Scanner and BufferedReader performance comparison

AC code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {
    
    

	public static void main(String[] args) throws IOException {
    
    
		BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
		//Scanner input2 = new Scanner(System.in);
		String [] lengths = input.readLine().split(" ");
		int ra = Integer.parseInt(lengths[0]);
		int ca = Integer.parseInt(lengths[1]);
		int A[][] = new int[ra][ca];
		for (int i = 0; i < ra; i++) {
    
    
			String datas[] = input.readLine().split(" ");
			for (int j = 0; j < ca; j++) {
    
    
				A[i][j] = Integer.parseInt(datas[j]);
			}
		}
		
		String [] len2 = input.readLine().split(" ");
		int rb = Integer.parseInt(len2[0]);
		int cb = Integer.parseInt(len2[1]);
		int B[][] = new int[rb][cb];
		for (int i = 0; i < rb; i++) {
    
    
			String datas[] = input.readLine().split(" ");
			for (int j = 0; j < cb; j++) {
    
    
				B[i][j] = Integer.parseInt(datas[j]);
			}
		}
		
		if (ca == rb) {
    
    
			int C[][] = new int[105][105];
			for (int i = 0; i < ra; i++) {
    
    
				for (int j = 0; j < cb; j++) {
    
    
					for (int k = 0; k < rb; k++) {
    
    
						C[i][j] += A[i][k] * B[k][j];
					}
				}
			}
			System.out.println(ra + " " + cb);
			for (int i = 0; i < ra; i++) {
    
    
				for (int j = 0; j < cb; j++) {
    
    
					if (j == cb - 1) {
    
    
						System.out.println(C[i][j]);
					} else {
    
    
						System.out.print(C[i][j] + " ");
					}
				}
			}
		} else {
    
    
			System.out.println("Error: " + ca + " != " + rb);
		}
		input.close();
	}
}

Guess you like

Origin blog.csdn.net/qq_45880043/article/details/108717649