Matrix Multiplication解题报告
2013-07-12 21:58
225 查看
题目摘要: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 weknow matrix a (m rows and n columns) and b (n rows and t
columns), the result matrix c (m rows and tcolumns) can be calculated by expression c (i, j) =
(1<=
i<= m, 1 <= j <= t). But John wants to make things difficult. He wantsJohnny to calculate matrix c using expression c (i, j) =
(1<=
i <= m, 1 <= j<= t, i + k is odd and k +j is even). We consider that 2 is odd and even. Atthe beginning, c (i, j) =0.
题目大意:重新定义矩阵乘法,在原来的条件下增加了条件,要求i+k是奇数,k+j是偶数时才加上。并且规定,2既是奇数又是偶数。
输入输出要求:
Input
The first line of input is the number T oftest case.
The first line of each test case containsthree integers, m, n, t, indicates the size of the matrix a and b. We alwaysassume 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 tintegers, 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. Thenumbers 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
解题思路:两个矩阵相乘,3重for循环加上新条件一血AC。
代码:
#include<iostream>
#include<cstring>
using namespace std;
const int maxn=100+5;
int A[maxn][maxn];
int B[maxn][maxn];
int C[maxn][maxn];
int main()
{
intT;
inti,j,k;
cin>>T;
while(T--)
{
memset(C,0,sizeof(C));
intm,n,t;
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)&&((k+j)%2==0))
C[i][j]=C[i][j]+A[i][k]*B[k][j];
else
if(((i+k)%2)&&((k+j)%2==0))
C[i][j]=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<<" ";
}
cout<<endl;
}
}
return0;
}
解题感想:挺简单一题,千万注意There is no space at the end of each line.这句话就好,PE的话就抓狂了
Recently, he is reviewing Linear Algebra.Johnny always says that he is very skillful at matrix
multiplication. As we all know that, if weknow matrix a (m rows and n columns) and b (n rows and t
columns), the result matrix c (m rows and tcolumns) can be calculated by expression c (i, j) =
(1<=
i<= m, 1 <= j <= t). But John wants to make things difficult. He wantsJohnny to calculate matrix c using expression c (i, j) =
(1<=
i <= m, 1 <= j<= t, i + k is odd and k +j is even). We consider that 2 is odd and even. Atthe beginning, c (i, j) =0.
题目大意:重新定义矩阵乘法,在原来的条件下增加了条件,要求i+k是奇数,k+j是偶数时才加上。并且规定,2既是奇数又是偶数。
输入输出要求:
Input
The first line of input is the number T oftest case.
The first line of each test case containsthree integers, m, n, t, indicates the size of the matrix a and b. We alwaysassume 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 tintegers, 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. Thenumbers 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
解题思路:两个矩阵相乘,3重for循环加上新条件一血AC。
代码:
#include<iostream>
#include<cstring>
using namespace std;
const int maxn=100+5;
int A[maxn][maxn];
int B[maxn][maxn];
int C[maxn][maxn];
int main()
{
intT;
inti,j,k;
cin>>T;
while(T--)
{
memset(C,0,sizeof(C));
intm,n,t;
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)&&((k+j)%2==0))
C[i][j]=C[i][j]+A[i][k]*B[k][j];
else
if(((i+k)%2)&&((k+j)%2==0))
C[i][j]=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<<" ";
}
cout<<endl;
}
}
return0;
}
解题感想:挺简单一题,千万注意There is no space at the end of each line.这句话就好,PE的话就抓狂了
相关文章推荐
- Gossiping --解题报告
- LeetCode Find Minimum in Rotated Sorted Array 解题报告
- poj2996解题报告
- Leetcode 389. Find the Difference 找不同 解题报告
- [LeetCode 解题报告]005.Longest Palindromic Substring
- SDUT 3648 迷の有序序列 (动态规划) -- 解题报告
- [LeetCode 解题报告]008.String to Integer (atoi)
- JOJ 1051: Flip and Shift 解题报告
- C2第五次作业解题报告
- 差分约束POJ3159Candies(spfa+stack)解题报告
- [Leetcode] 154. Find Minimum in Rotated Sorted Array II 解题报告
- [Leetcode] 609. Find Duplicate File in System 解题报告
- FOJ--1733--Image Distortion--解题报告
- Codeforces Round #190 (Div. 2) 解题报告
- USACO Cow XOR 解题报告
- 查分约束POJ3169Layout解题报告
- POJ 2828 解题报告
- 2016.9.3测试解题报告(NOIP2014 day1 day2)
- J - Perfect Pth Powers解题报告
- 第三届蓝桥杯C++本科B组决赛解题报告(更新中)