Matrix Multiplication
来源:互联网 发布:网络格斗游戏 编辑:程序博客网 时间:2024/05/22 17:33
Problem H: Matrix Multiplication
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 84 Solved: 33
Description
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.
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 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.
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.
Sample Input
1
2 2 2
1 2
2 3
2 3
3 0
Sample Output
2 0
4 0
简单题,矩阵相乘运用。
代码:
#include<iostream>#include<stdio.h>#include<cstring>using namespace std;#define MAX 100int a[MAX+5][MAX+5];int b[MAX+5][MAX+5];int c[MAX+5][MAX+5];int main(){ int aa,m,n,t,i,j,k; scanf("%d",&aa); while(aa--) { scanf("%d%d%d",&m,&n,&t); memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); for(i=1;i<=m;i++) for(j=1;j<=n;j++) scanf("%d",&a[i][j]); for(i=1;i<=n;i++) for(j=1;j<=t;j++) scanf("%d",&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)&&(j+k==2||(j+k)%2==0)) c[i][j]+=a[i][k]*b[k][j]; for(i=1;i<=m;i++) { for(j=1;j<=t;j++) { if(j==1) printf("%d",c[i][j]); else printf(" %d",c[i][j]); } printf("\n"); } } return 0;}
FROM:暑假训练第三场
- Matrix Multiplication
- Matrix Multiplication
- Matrix Multiplication
- Matrix Multiplication
- Matrix multiplication
- Matrix multiplication
- Matrix Chain Multiplication
- zoj1094 Matrix Chain Multiplication
- Matrix chain multiplication problem
- POJ 3318 Matrix Multiplication
- Matrix Chain Multiplication
- uva442 Matrix Chain Multiplication
- 3712. Matrix multiplication
- 442 - Matrix Chain Multiplication***
- poj Matrix Chain Multiplication
- poj 2890 Matrix Multiplication
- 442 - Matrix Chain Multiplication
- sgu 196 Matrix Multiplication
- UVA11729 Commando War
- Winform 让跨线程访问变得更简单
- 深入浅出 Cocoa 多线程编程之 block 与 dispatch quene
- Hadoop版本:hadoop-0.20.2 Eclipse版本:eclipse-java-helios-SR2-linux-gtk.tar.gz
- errors when vc6.0 to VS2008
- Matrix Multiplication
- 运用VS2008打包MSI程序安装包的更新
- 关于Zbar和ZXing这两个无比强大的二维码和条形码识别工具 .
- android aidl
- 获取Android手机的IP地址
- win8 下ie10启动就显示停止工作的解决办法
- 当异常出现时
- Install Theano in Linux without root authority (sodu)
- 5.4上安装Oracle 10g时提示如下错误: