3712. Matrix multiplication
| |||||||||
| |||||||||
Description
Given two n*n matrices, A and B, computes its matrices multiplication C=A∙B.
Input
There are multiples test cases. Each case is:
The first line is an integer n(<=10), meaning the size of the matrix. For the following 2*n lines, each line contains n integers. The first n lines contain the elements in matrix A.
Input is terminated by EOF.
Output
For each test case, output:
The multiplication C= A∙B, there will be n lines and each line contains n integers, separating the integers by a space.
Sample Input
2
1 2
3 4
1 1
1 1
Sample Output
3 3
7 7
// source code of submission 962891, Zhongshan University Online Judge System
#include <iostream>
using namespace std;
int main ()
{
int n;
while (cin >> n)
{
int a1[100][100],a2[100][100],a3[100][100];
for (int i = 0 ; i < n ; i++)
{
for (int j = 0 ; j < n ; j++)
{
cin >> a1[i][j];
}
}
for (int i = 0 ; i < n ; i++)
{
for (int j = 0 ; j < n ; j++)
{
cin >> a2[i][j];
}
}
for (int i = 0 ; i < n ; i++)
{
for (int j = 0 ; j < n ; j++)
{
a3[i][j] = 0;
}
}
for (int i = 0; i < n ; i++)
{
for (int j = 0 ; j < n ; j++)
{
for (int k = 0; k < n ; k++)
{
a3[i][j] += (a1[i][k] * a2[k][j]);
}
}
}
for (int i = 0 ; i < n ; i++)
{
for (int j = 0 ; j < n ; j++)
{
if (j != n - 1)
cout << a3[i][j] << " ";
else
cout << a3[i][j];
}
cout << endl;
}
}
return 0;
}
Description
Given two n*n matrices, A and B, computes its matrices multiplication C=A∙B.
Input
There are multiples test cases. Each case is:
The first line is an integer n(<=10), meaning the size of the matrix. For the following 2*n lines, each line contains n integers. The first n lines contain the elements in matrix A.
Input is terminated by EOF.
Output
For each test case, output:
The multiplication C= A∙B, there will be n lines and each line contains n integers, separating the integers by a space.
Sample Input
2 1 2 3 4 1 1 1 1
Sample Output
3 3 7 7 |
// source code of submission 962891, Zhongshan University Online Judge System
#include <iostream>
using namespace std;
int main ()
{
int n;
while (cin >> n)
{
int a1[100][100],a2[100][100],a3[100][100];
for (int i = 0 ; i < n ; i++)
{
for (int j = 0 ; j < n ; j++)
{
cin >> a1[i][j];
}
}
for (int i = 0 ; i < n ; i++)
{
for (int j = 0 ; j < n ; j++)
{
cin >> a2[i][j];
}
}
for (int i = 0 ; i < n ; i++)
{
for (int j = 0 ; j < n ; j++)
{
a3[i][j] = 0;
}
}
for (int i = 0; i < n ; i++)
{
for (int j = 0 ; j < n ; j++)
{
for (int k = 0; k < n ; k++)
{
a3[i][j] += (a1[i][k] * a2[k][j]);
}
}
}
for (int i = 0 ; i < n ; i++)
{
for (int j = 0 ; j < n ; j++)
{
if (j != n - 1)
cout << a3[i][j] << " ";
else
cout << a3[i][j];
}
cout << endl;
}
}
return 0;
}