矩阵相乘

#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;
        }
    }
}

 

posted @ 2018-03-10 16:48  灬从此以后灬  阅读(164)  评论(0编辑  收藏  举报