Problem Description
enter two matrices, each of which is the m*s,s*n size. Outputs the result of multiplying the two matrices.
Input Format
The first line, a space separated by three positive integers m,s,n (none more than 200).
The next M line, an integer separated by a space of s per line, represents matrix A (I,J).
Next S line, an integer separated by n spaces per line, represents matrix B (i,j).
output Format
m line, an integer separated by n spaces per line, and the value of Matrix C (i,j) after the output is multiplied.
Sample Input
2 3 2
1 0-1
1 1-3
0 3
1 2
3 1
Sample Output
-3 2
-8 2
Tips
The matrix C should be a M row n column, where C (i,j) equals the inner product of the line vector of line I of matrix A and the matrix B J column vector.
For example, C (All) = (1,0,-1) * (0,1,3) = 1 * 0 +0*1+ (-1) *3=-3
Problem Solving Analysis:
The topic here is the basic method of linear algebra, if you do not know how to find the matrix product of Baidu a bit.
Code:
#include <iostream>
#include <cstring>
using namespace Std;
int main ()
{
int m,s,n;
cin>>m>>s>>n;
int a[m+1][s+1];
int b[s+1][n+1];
int c[m+1][n+1];
Memset (C,0,sizeof (c));
int i,j,k;
for (i=1;i<=m;i++)
for (j=1;j<=s;j++)
cin>>a[i][j];
for (i=1;i<=s;i++)
for (j=1;j<=n;j++)
cin>>b[i][j];
for (k=1;k<=s;k++)
for (i=1;i<=m;i++)
for (j=1;j<=n;j++)
C[I][J]+=A[I][K]*B[K][J];
for (i=1;i<=m;i++)
{
for (j=1;j<=n;j++)
{
cout<<c[i][j];
if (j!=n)
cout<< "";
}
cout<<endl;
}
return 0;
}
Matrix multiplication---Blue bridge cup