题解 | #矩阵乘法#
矩阵乘法
https://www.nowcoder.com/practice/ebe941260f8c4210aa8c17e99cbc663b
#include <iostream> #include <vector> using namespace std; int compute_one(vector<int>& M1, vector<int>& M2) { int l = M1.size(); int res = 0; for (int i = 0; i < l; i++) { res += M1[i] * M2[i]; } return res; } int main() { int h, c, z; cin >> h >> c >> z; vector<vector<int>> M1(h, vector<int>(c)); vector<vector<int>> M2(z, vector<int>(c)); for (int i = 0; i < h; i++) { for (int j = 0; j < c; j++) { cin >> M1[i][j]; } } for (int i = 0; i < c; i++) { for (int j = 0; j < z; j++) { cin >> M2[j][i]; } } /* for (auto l : M1) { for (auto a : l) { cout << a << ' '; } cout << endl; } for (auto l : M2) { for (auto a : l) { cout << a << ' '; } cout << endl; } */ vector<vector<int>> res(h, vector<int>(z)); for (int i = 0; i < h; i++) { for (int j = 0; j < z; j++) { res[i][j] = compute_one(M1[i], M2[j]); } } for (auto l : res) { for (auto a : l) { cout << a << ' '; } cout << endl; } } // 64 位输出请用 printf("%lld")
把第二个矩阵重新排列一下
#华为OD机试真题#