Matrix Multiplication

Matrix Multiplication

题目描述

Johnny and John are good friends. Johnny is going to take the entrance exams for postgraduate schools. 
Recently, he is reviewing Linear Algebra. Johnny always 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) = a(i,k)*b(k,j),1<=k<=n.(1<= i <= m, 1 <= j <= t).
But John wants to make things difficult. He wants Johnny to calculate matrix c using expression
c (i, j) = a(i,k)*b(k,j) (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. 

输入

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 matrix a. Each line contains n integers. 
Then n lines followed, each line contains t integers, describe the matrix b. 
1<= T <= 10, 1 <= m, n, t <= 100, 0 <= a (i, j) <= 100, 0 <= b (i, j) <= 100. 

输出

For each test case output the matrix c. The numbers are separated by spaces. There is no space at the end of 
each line. 

样例输入

1 2 2 2 1 2 2 3 2 3 3 0

样例输出

2 0 4 0

代码如下

#include<iostream>

using namespace std;
int a[105][105];
int b[105][105];
int c[105][105];
int main()
{
   int m,n,t,k,s,i,j;
   cin>>k;//测试数据的个数
   while(k--)
   {
  cin>>m>>n>>t;
  for(i=1;i<=m;i++)
  {
  for(j=1;j<=n;j++)
  cin>>a[i][j];//输入矩阵a;
  }
  for(i=1;i<=n;i++)
  {
  for(j=1;j<=t;j++)
  cin>>b[i][j];//输入矩阵b;
  }
  
for (i=1;i<=m;i++)
{
for (j=1;j<=t; j++)
{     c[i][j]=0;
for (s=1; s<=n; s++)
{
if (((i+s)%2==1||(i+s==2))&&((j+s)%2==0||(j+s==2)))
{
c[i][j]+=a[i][s]*b[s][j];
}
}
}
}
        for(i=1;i<=m;i++)
{
for(int j=1;j<=t-1;j++)
{
cout<<c[i][j]<<" ";
}
cout<<c[i][t]<<endl;
}
}


return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七刀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值