[第三次训练]Matrix Multiplication
来源:互联网 发布:德温特专利数据库 编辑:程序博客网 时间:2024/05/16 08:19
Matrix Multiplication
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
HINT
解题报告
此题是要求输入m,n,t三个数,分别构成m*n与n*t 两个矩阵,然后利用公式的矩阵乘法方式得到新的矩阵m*t,并把它输出。并且规定i+k要为奇数,j+k要为偶数。2即为奇数页为偶数。所以只需判断((i+k)%2==1||i+k==2)&&(j+k)%2==0即可,满足的就加入相应的c(i,j),然后输出即可(注意输出要按照要求,不要多空格或者换行符,本人因为条件写错,每行多了一个空格,导致PE2次),代码如下:#include<stdio.h>#include<string.h>long long a[110][110],b[110][110],c[110][110];int main(){ int m,n,t,T,k,i,j; scanf("%d",&T); while(T--) { memset(c,0,sizeof(c)); scanf("%d%d%d",&m,&n,&t); for(i=1;i<=m;i++) for(j=1;j<=n;j++) scanf("%lld",&a[i][j]); for(i=1;i<=n;i++) for(j=1;j<=t;j++) scanf("%lld",&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==1||i+k==2)&&((j+k)%2==0||j+k==2)) c[i][j]+=a[i][k]*b[k][j]; } for(i=1;i<=m;i++) { for(j=1;j<=t;j++) { printf("%lld",c[i][j]); if(j!=t) printf(" "); else printf("\n"); } } } return 0;}
- [第三次训练]Matrix Multiplication
- [第三次训练]Matrix Operation
- 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
- HDOJ 4540 威威猫系列故事——打地鼠 杭电 ACM
- 基于epoll的聊天室程序
- firefox应用 插件
- Hamilton
- Cocos2d-x中按钮类CCControlButton的使用
- [第三次训练]Matrix Multiplication
- vi的工作模式及各模式之间如何相互切换
- opencv学习之路(2)--(测试例程)
- opencv学习之路(3)--(图像的读入和显示)
- 究竟应该采用“深搜”(DFS)还是“广搜”(BFS)?
- opencv学习之路(4)--(图像创建复制和保存)
- java无难事笔记(七)
- Java学习(四)
- 深入浅出Mybatis-sql自动生成