[HPUVJ - 1715] 矩陣乘法

Link:點擊打開鏈接


Time Limit: 1000MS   Memory Limit: 524288KB   64bit IO Format: %lld & %lld

 Status

Description

輸入兩個矩陣,分別是m*s,s*n大小。輸出兩個矩陣相乘的結果。

Input

 第一行,空格隔開的三個正整數m,s,n(均不超過200)。
  接下來m行,每行s個空格隔開的整數,表示矩陣A(i,j)。
  接下來s行,每行n個空格隔開的整數,表示矩陣B(i,j)。

Output

m行,每行n個空格隔開的整數,輸出相乘後的矩陣C(i,j)的值。

Sample Input

2 3 2
1 0 -1
1 1 -3
0 3
1 2
3 1

Sample Output

-3 2
-8 2

Hint

矩陣C應該是m行n列,其中C(i,j)等於矩陣A第i行行向量與矩陣B第j列列向量的內積。
例如樣例中C(1,1)=(1,0,-1)*(0,1,3) = 1 * 0 +0*1+(-1)*3=-3

Source

藍橋杯算法訓練


Code:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
int main()
{
	int m,s,n;
	int a[205][205],b[205][205],c[205][205];
	scanf("%d%d%d",&m,&s,&n);
	for(int i=1;i<=m;i++)
		for(int j=1;j<=s;j++)
			scanf("%d",&a[i][j]);
	for(int i=1;i<=s;i++)
		for(int j=1;j<=n;j++)
			scanf("%d",&b[i][j]);
	for(int i=1;i<=m;i++)
		for(int j=1;j<=n;j++)
		{
			for(int k=1;k<=s;k++)
				c[i][j]+=a[i][k]*b[k][j];
		}
	for(int i=1;i<=m;i++)
		for(int j=1;j<=n;j++)
		{
			printf("%d%c",c[i][j],j==n? '\n':' ');
		}
return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章