3712. Matrix multiplication
Constraints
Time Limit: 1 secs, Memory Limit: 256 MB
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
Hint
//To process multiple cases, you can use some C++ codes like:
//original codes:
// cin >> a >> b;
// cout << a + b << endl;
while(cin >> a){
cin >> b;
cout << a + b << endl;
}
//or use some C codes like:
//original codes:
// scanf("%d%d", &a, &b);
// printf("%d\n", a + b);
while(scanf("%d", &a) != EOF){
scanf("%d", &b);
printf("%d\n", a + b);
}
// Problem#: 3712
// Submission#: 1984646
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<iostream>
#include<stdio.h>
#include<string>
#include<cmath>
using namespace std;
int main(){
int n,i;
while(scanf("%d",&n)!=EOF){
int x,y;
int **a,**b;
a=new int*[n];
b=new int*[n];
for(x=0;x<n;x++){
a[x]=new int [n];
b[x]=new int [n];
}
for(x=0;x<n;x++){
for(y=0;y<n;y++){
cin>>a[x][y];
}
}
for(x=0;x<n;x++){
for(y=0;y<n;y++){
cin>>b[x][y];
}
}
int sum=0,z=0;
for(x=0;x<n;x++){
for(y=0;y<n-1;y++){
for(z=0;z<n;z++){
sum+=a[x][z]*b[z][y];
}
cout<<sum<<" ";
sum=0;
}
for(z=0;z<n;z++){
sum+=a[x][z]*b[z][y];
}
cout<<sum<<endl;
sum=0;
}
}
return 0;
}