poj 3318 Matrix Multiplication

时间:2020-11-28 16:23:04

http://poj.org/problem?id=3318

矩阵A*矩阵B是否等于矩阵C

 #include <cstdio>
#include <cstring>
#include <time.h>
#include <algorithm>
#define maxn 1010
using namespace std; int a[maxn][maxn],b[maxn][maxn],c[maxn][maxn],d[maxn];
int n;
int b1[maxn],c1[maxn],a1[maxn]; int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=; i<n; i++)
{
for(int j=; j<n; j++)
scanf("%d",&a[i][j]);
}
for(int i=; i<n; i++)
{
for(int j=; j<n; j++)
{
scanf("%d",&b[i][j]);
}
}
for(int i=; i<n; i++)
{
for(int j=; j<n; j++)
{
scanf("%d",&c[i][j]);
}
}
srand((unsigned int)time());
for(int i=; i<n; i++)
{
d[i]=rand()%;
}
for(int i=; i<n; i++)
{
for(int j=; j<n; j++)
{
b1[i]+=b[i][j]*d[j];
c1[i]+=c[i][j]*d[j];
}
}
for(int i=; i<n; i++)
{
for(int j=; j<n; j++)
{
a1[i]+=a[i][j]*b1[j];
}
}
bool flag=false;
for(int i=; i<n; i++)
{
if(a1[i]!=c1[i])
{
flag=true;
break;
}
}
if(!flag) printf("YES\n");
else printf("NO\n");
}
return ;
}