一本通1641【例 1】矩阵 A×B

1641: 【例 1】矩阵 A×B

sol:矩阵乘法模板。三个for循环

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
    ll s=0;
    bool f=0;
    char ch=' ';
    while(!isdigit(ch))
    {
        f|=(ch=='-'); ch=getchar();
    }
    while(isdigit(ch))
    {
        s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
    }
    return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
    if(x<0)
    {
        putchar('-'); x=-x;
    }
    if(x<10)
    {
        putchar(x+'0');    return;
    }
    write(x/10);
    putchar((x%10)+'0');
    return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=105;
int n,m,p,a[N][N],b[N][N],c[N][N];
int main()
{
    int i,j,k;
    R(n); R(m);
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++) R(a[i][j]);
    }
    R(p);
    for(i=1;i<=m;i++)
    {
        for(j=1;j<=p;j++) R(b[i][j]);
    }
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=p;j++)
        {
            for(k=1;k<=m;k++) c[i][j]+=a[i][k]*b[k][j];
        }
    }
    for(i=1;i<=n;i++,putchar('\n'))
    {
        for(j=1;j<=p;j++) W(c[i][j]);
    }
    return 0;
}
/*
input
2 3
1 2 3
3 2 1
2
1 1
2 2
3 3
output
14 14
10 10
*/
View Code

猜你喜欢

转载自www.cnblogs.com/gaojunonly1/p/10486484.html