矩阵乘法——矩阵A×B

问题 A: 【例题1】矩阵A×B

时间限制: 1 Sec  内存限制: 128 MB
提交: 9  解决: 6
[提交][状态][讨论版][命题人:quanxing]

题目描述

矩阵A规模是n×m,矩阵B规模是m×p,现在需要你求A*B

输入

输入n,m。然后输入n×m的矩阵。

输入p,然后输入m×p的矩阵。

1<=n,m,p<=100

-10000<=矩阵元素<=10000

输出

输出相乘后的n×p的矩阵

样例输入

2 3
1 2 3
3 2 1
2
1 1
2 2
3 3

样例输出

14 14
10 10
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
int n,m,p;
int a[105][105],b[105][105],c[105][105];
int main()
{
    cin>>n>>m;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        cin>>a[i][j];
    cin>>p;
    for(int i=0;i<m;i++)
        for(int j=0;j<p;j++)
        cin>>b[i][j];
    memset(c,0,sizeof(c));
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            for(int k=0;k<p;k++)
            c[i][k]+=a[i][j]*b[j][k];//c第i行第j列的值是a第i行和b第k列对应值乘积之和
    for(int i=0;i<n;i++)
    {
        cout<<c[i][0];
      for(int j=1;j<p;j++)
        cout<<" "<<c[i][j];
      cout<<endl;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40729773/article/details/81407892
今日推荐