首页 > 其他 > 详细

矩阵相乘

时间:2018-03-10 16:54:30      阅读:197      评论:0      收藏:0      [点我收藏+]
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <vector>
using namespace std;
const int maxn = 1e4+10;
const int maxm = 1e4+10;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
const double epx = 1e-10;
typedef long long ll;
const ll INF = 1e18;
int a[maxn][maxn];
int b[maxn][maxn];
int c[maxn][maxn];
int main()
{
    int n,m,n1,m1;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
            cin>>a[i][j];
    }
    cin>>n1>>m1;
    for(int i=1;i<=n1;i++)
    {
        for(int j=1;j<=m1;j++)
            cin>>b[i][j];
    }
    if(m!=n1)
        cout<<"Error: "<<m<<" != "<<n1<<endl;
    else
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m1;j++)
            {
                int sum=0;
                for(int k=1;k<=m;k++)
                {
                    sum+=a[i][k]*b[k][j];
                }
                c[i][j]=sum;
            }
        }
        cout<<n<<" "<<m1<<endl;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<m1;j++)
            {
                cout<<c[i][j]<<" ";
            }
            cout<<c[i][m1]<<endl;
        }
    }
}

 

矩阵相乘

原文:https://www.cnblogs.com/stranger-/p/8540623.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!