又见矩阵!!我只能说矩阵的题目最难搞了。
Johnnyand Johnare good friends. Johnnyis going to take the entrance exams for postgraduate schools.
Recently, he is reviewing Linear Algebra. Johnnyalways says that he is very skillful at matrix
multiplication. As we all know that, if we know matrix a (m rows and n columns) and b (n rows and t
columns), the result matrix c (m rows and t columns) can be calculated by expression
c(i,j)=
But Johnwants to make things difficult. He wants Johnnyto
calculate matrix c using expression
( (1<= i <= m, 1 <= j <= t, i + k is odd and k + j is even). We consider that 2 is odd and even. At the beginning, c (i, j) =0.
Input
The first line of input is the number T of test case.
The first line of each test case contains three integers, m,n, t, indicates the size of the matrix a and b. We
always assume that the rows of matrix a equals to matrix b's columns.
The next m lines describe the matrixa. Each line contains n integers.
Then n lines followed, each line containst integers, describe the matrix b.
1<= T <= 10, 1 <= m, n, t <= 100, 0 <= a (i, j) <= 100, 0 <= b (i, j) <= 100.
Output
For each test case output the matrix c. The numbers are separated by spaces.There is no space at the end of
each line.
矩阵类型的题目还是要多做,多理解啊。值得一说的是这道题的输出有点坑,那个换行和空格搞了好久啊!
#include<stdio.h>
#include <iostream>
using namespace std;
#include <string.h>
long long A[111][111],B[111][111],C[111][111];
int main()
{
int T,m,n,t;
int i,j,k;
cin>>T;
while(T--)
{
memset(C,0,sizeof(C));
memset(A,0,sizeof(A));
memset(B,0,sizeof(B));
cin>>m>>n>>t;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
cin>>A[i][j];
for(i=1;i<=n;i++)
for(j=1;j<=t;j++)
cin>>B[i][j];
for(i=1;i<=m;i++)
for(j=1;j<=t;j++)
for(k=1;k<=n;k++)
if((i+k==2||(i+k)%2==1)&&((k+j)%2==0))
{
C[i][j]+=A[i][k]*B[k][j];
}
for(i=1;i<=m;i++)
for(j=1;j<=t;j++)
{
cout<<C[i][j];
if(j!=t)
cout<<" ";
else
cout<<endl;
}
}
return 0;
}