Files
python/TangDou/AcWingSuanFaJinJieZhiNan/MatrixMul_1.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

32 lines
980 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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*m(n行m列)
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> a[i][j];
cin >> p; //矩阵b为m*p(m行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*p(n行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;
}