【codevs1287】矩阵乘法(矩阵乘法)

题目描述

传送门

题解

矩阵乘法(暴力)。

代码

#include
#include
#include
using namespace std;

const int max_n=205;
const int max_m=205;

int n1,m1,n2,m2;
int a[max_n][max_m],b[max_n][max_m],c[max_n][max_m];

int main(){
    scanf("%d%d",&n1,&m1);
    for (int i=1;i<=n1;++i)
      for (int j=1;j<=m1;++j)
        scanf("%d",&a[i][j]);
    scanf("%d%d",&n2,&m2);
    for (int i=1;i<=n2;++i)
      for (int j=1;j<=m2;++j)
        scanf("%d",&b[i][j]);
    for (int i=1;i<=n1;++i)
      for (int j=1;j<=m2;++j)
        for (int k=1;k<=n2;++k)
          c[i][j]+=a[i][k]*b[k][j];
    for (int i=1;i<=n1;++i)
      for (int j=1;j<=m2;++j)
        printf("%d%c",c[i][j]," \n"[j==m2]);
}

你可能感兴趣的:(题解,矩阵)