注意矩阵相乘需要三重循环来计算。
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 110;
int n, m, p;
int A[N][N], B[N][N];
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i ++ )
for (int j = 0; j < m; j ++ )
scanf("%d", &A[i][j]); //读入数组A
cin >> p;
for (int i = 0; i < m; i ++ )
for (int j = 0; j < p; j ++ )
scanf("%d", &B[i][j]); //读入数组B
for (int i = 0; i < n; i ++ )
{
for (int j = 0; j < p; j ++ )
{
int res = 0;
for (int k = 0; k < m; k ++ )
{
res += A[i][k] * B[k][j]; //三重循环输出结果
}
cout << res << ' ';
}
cout << endl;
}
return 0;
}