Multiplying Matrices
Source : SCU Programming Contest 2005 Final | |||
Time limit : 1 sec | Memory limit : 32 M |
Submitted : 2012, Accepted : 633

Input
The first line of input is the number of all the test cases that follow. For each test case, there are three positive integer m, n, p (m, n, p <= 100) on the first line, meaning that A's dimension is m rows by n columns while B's is n rows by p columns. Then comes the data of matrices A and B. Matrix A is represented by m lines of integers ranging in [-1000, 1000], with each line containing n integers separated by a space. A line is corresponding to a row of a matrix. The data format for matrix B is much the same except for its potentially different dimensions. Please refer to the section Sample Input.
Output
For each test case, print the product of A multiplied by B in the form described in the section Input, but do not include any dimension description.
Sample Input
2 2 2 2 1 2 3 4 1 2 3 4 1 2 1 1 0 0 1Sample Output
7 10 15 22 0
Author: hewei
/*
题意:两个矩阵的乘法
思路:算出来打印就行
注意每行输出的时候最后没有空格
*/
#include <iostream>
#include<stdio.h>
#include<cmath>
#define MAX 1000
int Matrices1[MAX+1][MAX+1],Matrices2[MAX+1][MAX+1],ANS[MAX+1][MAX+1];
using namespace std;
int main()
{
int N,n,m,p,i,j,k,sum;
cin >> N;
while(N--)
{
cin >> n >> m >> p;
for(i=0; i<n; i++)
for(j=0; j<m; j++)
cin >> Matrices1[i][j];
for(i=0; i<m; i++)
for(j=0; j<p; j++)
cin >> Matrices2[i][j];
for (i=0; i<n; i++)
{
for (j=0; j<p; j++)
{
sum=0;
for (k=0; k<m; k++)
{
sum+=Matrices1[i][k]*Matrices2[k][j];
}
ANS[i][j]=sum;
}
}
for(i=0; i<n; i++)
{
for(j=0; j<p-1; j++)
{
cout << ANS[i][j] << " ";
}
cout << ANS[i][p-1];
cout << endl;
}
}
return 0;
}