You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.4 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int n, m, k;
int a[N][N], b[N][N], c[N][N];
// 矩阵乘法模板
void mul(int a[][N], int b[][N], int c[][N]) {
// 注意这里的临时数组t绝不是画蛇添足
// 因为调用的时候有时会传递mul(a,b,b)这样的东东如果每次memset(c,0,sizeof c),
// 就会造成b在运行前被清空导致结果错误
// 代码解释:
// 从结果出发理解:
// C(i,j) =A(i,1)×B(1,j)+A(i,2)×B(2,j) +...A(r,1)×B(r,j)
// 抽象一下:
// C(i,j) = C(i,j) + A(i,k)× B(k,j)
int t[N][N] = {0};
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
for (int k = 0; k < N; k++)
t[i][j] = t[i][j] + a[i][k] * b[k][j]; // 矩阵乘法
memcpy(c, t, sizeof t);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("B2105.in", "r", stdin);
#endif
cin >> n >> m >> k;
// A矩阵 n*m
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> a[i][j];
// B矩阵m*k
for (int i = 1; i <= m; i++)
for (int j = 1; j <= k; j++)
cin >> b[i][j];
// 矩阵乘法
mul(a, b, c);
// 输出结果,控制格式
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= k; j++)
printf("%d ", c[i][j]);
printf("\n");
}
return 0;
}