题解 | #矩阵乘法#

矩阵乘法

https://www.nowcoder.com/practice/ebe941260f8c4210aa8c17e99cbc663b

#include <iostream>
using namespace std;
#include <vector>

void in(vector<vector<int>> &a, int x, int y)
{
    for (int i = 0; i < x; i++)
    {
        for (int j = 0; j < y; j++)
        {
            cin >> a[i][j];
        }
    }
}

void out(vector<vector<int>> &a, int x, int y)
{
    for (int i = 0; i < x; i++)
    {
        for (int j = 0; j < y; j++)
        {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
}

void caculate(const vector<vector<int>> &a, const vector<vector<int>> &b, vector<vector<int>> &res, int x, int y, int z)
{
    for (int i = 0; i < x; i++)
    {
        for (int j = 0; j < z; j++)
        {
            for (int k = 0; k < y; k++)
            {
                res[i][j] = res[i][j] + a[i][k] * b[k][j];
            }
        }
    }
}

int main()
{
    int x, y, z;
    cin >> x >> y >> z;

    vector<vector<int>> a(x, vector<int>(y, 0));
    vector<vector<int>> b(y, vector<int>(z, 0));
    vector<vector<int>> res(x, vector<int>(z, 0));
    in(a, x, y);
    in(b, y, z);
    caculate(a,b,res,x,y,z);
    out(res, x, z);
}

死去的线性代数忽然攻击我

华为机试刷题记录 文章被收录于专栏

记录一下手打代码的解题思路方便复习

全部评论

相关推荐

09-26 19:45
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务