L1-048 matrix A multiplied by B (15 points)

Given two matrices A and B, you are required to calculate their product matrix AB. It should be noted that only scale matched matrices can be multiplied. If A has R
​a
Line, C
​a
Column B has R
​b
Line, C
​b
Column, only C
​a
And R
​b
When  ̄ is equal, two matrices can be multiplied.

Input format:
The input gives two matrices A and B successively. For each matrix, the row number R and column number C are given in one row first, then row R, each row gives C integers, separated by one space, and there is no extra space at the beginning and end of the row. The input ensures that the R and C of both matrices are positive and that the absolute value of all integers does not exceed 100.

Output format:
If the scale of the two input matrices is matched, then output the product matrix AB according to the input format, otherwise output error: Ca! = Rb, where Ca is the number of columns of A and Rb is the number of rows of B.

Enter 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
Enter example 2:
3 2
38 26
43 -5
0 17
3 2
-11 57
99 68
81 72
Output example 2:
Error: 2 != 3

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<string>
#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<queue>
#define ll long long
#define dd double
using namespace std;

ll matrix_A[1050][1050];
ll matrix_B[1050][1050];

int main() {
	ll matrix_A_line;
	ll matrix_A_list;
	ll matrix_B_line;
	ll matrix_B_list;
	ll sum;
	cin >> matrix_A_line >> matrix_A_list;
	for (ll i = 0; i < matrix_A_line; i++) {
		for (ll j = 0; j < matrix_A_list; j++) {
			cin >> matrix_A[i][j];
		}
	}
	cin >> matrix_B_line >> matrix_B_list;
	for (ll i = 0; i < matrix_B_line; i++) {
		for (ll j = 0; j < matrix_B_list; j++) {
			cin >> matrix_B[i][j];
		}
	}
	if (matrix_A_list != matrix_B_line) {
		cout << "Error: " << matrix_A_list << " != " << matrix_B_line << endl;
	}
	else {
		cout << matrix_A_line << " " << matrix_B_list << endl;
		for (ll i = 0; i < matrix_A_line; i++) {
			for (ll j = 0; j < matrix_B_list; j++) {
				sum = 0;
				for (ll k = 0; k < matrix_A_list; k++) {
					sum += matrix_A[i][k] * matrix_B[k][j];
				}
				if (j != 0) {
					cout << " ";
				}
				cout << sum;
			}
			cout << endl;
		}
	}
}

Posted by nhan on Thu, 14 Nov 2019 11:40:25 -0800