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.

32 lines
980 B

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int a[N][N], b[N][N], c[N][N];
int n, m, p;
int main() {
cin >> n >> m; //矩阵a为n*mn行m列
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> a[i][j];
cin >> p; //矩阵b为m*pm行p列
for (int i = 0; i < m; i++)
for (int j = 0; j < p; j++)
cin >> b[i][j];
//结果是n行p列的所以外面两层循环是n和p
//最后一层循环是m因为(n,m),(m,p),所以(i,k)一组,(k,j)一组
for (int i = 0; i < n; i++) //矩阵c是a与b相乘得到的
for (int j = 0; j < p; j++) // n*pn行p列
for (int k = 0; k < m; k++)
c[i][j] += a[i][k] * b[k][j]; //乘法再sum求和
//输出
for (int i = 0; i < n; i++) {
for (int j = 0; j < p; j++)
cout << c[i][j] << " ";
cout << endl;
}
return 0;
}