1127: 矩阵乘积

来源:互联网 发布:mac版office激活工具 编辑:程序博客网 时间:2024/06/06 19:30

Description

计算两个矩阵A和B的乘积。

Input

第一行三个正整数m、p和n,0<=m,n,p<=10,表示矩阵A是m行p列,矩阵B是p行n列;

接下来的m行是矩阵A的内容,每行p个整数,用空格隔开;

最后的p行是矩阵B的内容,每行n个整数,用空格隔开。

Output

输出乘积矩阵:输出占m行,每行n个数据,以空格隔开。

Sample Input

2 3 41 0 10 0 11 1 1 34 5 6 78 9 1 0

Sample Output

9 10 2 38 9 1 0

HINT

Source

#include<stdio.h>
void matrixcheng (int a[11][11] ,int b[11][11] ,int m, int p, int n)
{
int c[11][11];
int i,j;
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
c[i][j]=0;
for (int k=0;k<p;k++)
c[i][j]+=a[i][k]*b[k][j];
}
}


for (i=0;i<m;i++)
{
printf("%d",c[i][0]);
for (j=1;j<n;j++)
{
printf(" %d",c[i][j]);
}
printf("\n");


}
}
void main ()
{
int i,j;
int a[11][11],b[11][11];
int m,p,n;
scanf("%d%d%d",&m,&p,&n);


for (i=0;i<m;i++)
{
for (j=0;j<p;j++)
scanf("%d",&a[i][j]);
}


for (i=0;i<p;i++)
{
for (j=0;j<n;j++)
scanf("%d",&b[i][j]);
}
matrixcheng (a,b,m,p,n);


}
 

0 0
原创粉丝点击