【ZhengQing】[1127]Matrix Product

Problem Description

矩阵乘积
Time limit: 1 SecMemory limit: 128 MB

Description

Calculate the product of two matrices A and B.

Input

The first line contains three positive integers m, p, and n, with 0<=m,n,p<=10, indicating that matrix A has m rows and p columns, and matrix B has p rows and n columns;
The next m lines contain matrix A, with p integers per line separated by spaces;
The last p lines contain matrix B, with n integers per line separated by spaces.

Output

Output the product matrix: it should occupy m lines, with n values per line separated by spaces.

Sample Input

1
2
3
4
5
6
7
8
2 3 4

1 0 1
0 0 1

1 1 1 3
4 5 6 7
8 9 1 0

Sample Output

1
2
9 10 2 3
8 9 1 0

Problem Analysis

Matrix multiplication:
The value at the intersection of row m and column n of the result matrix equals the sum of the products of the corresponding entries in row m of the first matrix and column n of the second matrix.
Understanding matrix multiplication - Ruan Yifeng’s blog
You can also brute-force it with arrays,
but here we do it in a more advanced way:
overload operators on a struct,
so that we can perform struct multiplication directly,
and then just output the result.

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
int n;
int map[12][12];
bool IsUpperTriMatrix() {
    for(int i=1; i<n; i++)
        for(int j=0; j<i; j++)
            if(map[i][j])
                return false;
    return true;
}
int main() {
    while(scanf("%d",&n)!=EOF) {
        for(int i=0; i<n; i++)
            for(int j=0; j<n; j++)
                scanf("%d",&map[i][j]);
        printf("%s\n",IsUpperTriMatrix()?"YES":"NO");
    }
    return 0;
}

Problem link: ZZULI 1127 Matrix Product

Loading...