MENU

矩阵相乘

May 10, 2020 • Read: 488 • 笔记

/*
 * @Author: Mr.Sen
 * @LastEditTime: 2020-05-10 22:31:34
 * @Website: https://449293786.site
 * @Mr.Sen All rights reserved
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int lst1[20][20],lst2[20][20],ans[20][20];
int n,m,x,y;

int fun()
{
    for (int i=0;i<n;i++)
    {
        for (int j=0;j<y;j++)
        {
            for (int k=0;k<m;k++)
            {
                ans[i][j]+=lst1[i][k]*lst2[k][j];
            }
        }
    }
    return 0;
}
int main()
{
    scanf("%d%d%d%d",&n,&m,&x,&y);
    for (int i=0;i<n;i++)
    {
        for (int j=0;j<m;j++)
        {
            scanf("%d",&lst1[i][j]);
        }
    }
    for (int i=0;i<x;i++)
    {
        for (int j=0;j<y;j++)
        {
            scanf("%d",&lst2[i][j]);
        }
    }
    if (m==x)
    {
        fun();
        for (int i=0;i<n;i++)
        {
            for (int j=0;j<y;j++)
            {
                j!=y-1?printf("%d ",ans[i][j]):printf("%d\n",ans[i][j]);
            }
        }
    }
    else
    {
        printf("0\n");
    }
    
    return 0;
}
Archives Tip
QR Code for this page
Tipping QR Code