题目描述

In mathematics, matrix multiplication or matrix product is a binary operation that produces a matrix from two matrices with entries in a field, or, more generally, in a ring or even a semiring. The matrix product is designed for representing the composition of linear maps that are represented by matrices. Matrix multiplication is thus a basic tool of linear algebra, and as such has numerous applications in many areas of mathematics, as well as in applied mathematics, physics, and engineering. In more detail, if A is an n x m matrix and B is an m x p matrix, their product AB is an n x p matrix, in which the m entries across a row of are multiplied with the m emtries down a column of B and summed to produce an entry of AB. When two linear maps are represented by matrices, then the matrix product represents the composition of the two maps.

We can only multiply two matrices if their dimensions are compatible, which means the number of columns in the first matrix is the same as the number of rows in the second matrix. 
If A is an n x m matrix and B is an m x p matrix,
the matrix product C = AB is defined to be the n x p matrix
such that
,
for i = 1,2, ..., n and j = 1,2, ..., p.
Your task is to design a matrix multiplication calculator to multiply two matrices and
display the output. If the matrices cannot be multiplied, display "ERROR".

输入描述:

The first line of the input is T(1≤ T ≤ 100), which stands for the number of test cases you need to solve.
For each test case, the first line contains four integers m, n, p and q (1 ≤ m,n,p,q ≤ 20). m and n represent the dimension of matrix A, while p and q represent the dimension of matrix B.
The following m lines consist of the data for matrix A followed by p lines that contains the data for matrix B. (-100 ≤ aij≤ 100, -100 ≤ bij≤ 100).

输出描述:

For each test case, print the case number and the output of the matrix multiplication.

输入例子:
2
2 3 3 2
1 1 1
1 2 3
2 3
4 5
6 7
2 3 2 3
1 2 3
1 2 3
2 3 4
2 3 4
输出例子:
Case 1:
12 15
28 34
Case 2:
ERROR

-->

示例1

输入

2
2 3 3 2
1 1 1
1 2 3
2 3
4 5
6 7
2 3 2 3
1 2 3
1 2 3
2 3 4
2 3 4

输出

Case 1:
12 15
28 34
Case 2:
ERROR
解题思路:题意描述得很清楚,矩阵乘法,直接套公式即可。
AC代码:
 #include <bits/stdc++.h>
using namespace std;
const int maxn=;
struct Matrix
{
int m[maxn][maxn];
}init1,init2,c;
int t,row1,col1,row2,col2;
void mul(Matrix a,Matrix b){
for(int i=;i<row1;i++){//枚举第一个矩阵的行。
for(int j=;j<col2;j++){//枚举第二个矩阵的列。
c.m[i][j]=;//注意清0
for(int k=;k<col1;k++)//枚举个数
c.m[i][j]+=a.m[i][k]*b.m[k][j];
}
}
}
int main(){
cin>>t;
for(int cas=;cas<=t;++cas){
cin>>row1>>col1>>row2>>col2;
for(int i=;i<row1;++i)
for(int j=;j<col1;++j)
cin>>init1.m[i][j];
for(int i=;i<row2;++i)
for(int j=;j<col2;++j)
cin>>init2.m[i][j];
printf("Case %d:\n",cas);
if(col1!=row2)cout<<"ERROR"<<endl;
else{
mul(init1,init2);
for(int i=;i<row1;++i)
for(int j=;j<col2;++j)
cout<<c.m[i][j]<<(j==col2-?'\n':' ');
}
}
return ;
}

2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛-K-Matrix Multiplication(矩阵乘法)的更多相关文章

  1. 2018 ACM 国际大学生程序设计竞赛上海大都会部分题解

    题目链接 2018 ACM 国际大学生程序设计竞赛上海大都会 下午午休起床被同学叫去打比赛233 然后已经过了2.5h了 先挑过得多的做了 .... A题 rand x*n 次点,每次judge一个点 ...

  2. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it

    链接:https://www.nowcoder.com/acm/contest/163/F 来源:牛客网 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it 时间限制:C ...

  3. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it (扫描线)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it (扫描线) 链接:https://ac.nowcoder.com/acm/contest/163/F来源:牛客网 时间 ...

  4. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...

  5. 2018 ACM 国际大学生程序设计竞赛上海大都会赛

    传送门:2018 ACM 国际大学生程序设计竞赛上海大都会赛 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛2018-08-05 12:00:00 至 2018-08-05 17:00:0 ...

  6. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位dp)

    题目链接:https://ac.nowcoder.com/acm/contest/163/J 题目大意:给定一个数N,求区间[1,N]中满足可以整除它各个数位之和的数的个数.(1 ≤ N ≤ 1012 ...

  7. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 A,D

    A链接:https://www.nowcoder.com/acm/contest/163/A Fruit Ninja is a juicy action game enjoyed by million ...

  8. 2018 ACM 国际大学生程序设计竞赛上海大都会 F - Color it (扫描线)

    题意:一个N*M的矩形,每个点初始都是白色的,有Q次操作,每次操作将以(x,y)为圆心,r为半径的区域涂成黑点.求最后剩余白色点数. 分析:对每行,将Q次操作在该行的涂色视作一段区间,那么该行最后的白 ...

  9. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛-B-Perfect Numbers(完数)

    题目描述 We consider a positive integer perfect, if and only if it is equal to the sum of its positive d ...

随机推荐

  1. BFS简单迷宫

    常见迷宫: 输入迷宫 启点 终点 然后求最短路径 BFS例题 用dist[][]数组来记录 启点到每个点的最短路径 #include <iostream> #include <fst ...

  2. POJ 1064_Cable master

    题意: 给定一系列长度的电缆,将他们切成K条相等长度的电缆,问每条最长有多长? 分析: 对长度进行二分,判断能否满足K条. 二分搜索: 在求解最大化或最小化问题中,能够比较简单的判断条件是否满足,那么 ...

  3. 积累——SQLCommand命令

    SQLcommand表示要对SQL数据库运行的一个 T-SQL 语句或存储过程.以便运行大量操作或处理数据库结构. 在对数据库訪问的时候,就经经常使用到这个.看看它是怎么做到的吧! 一.属性 Comm ...

  4. 剑指Offer —— BFS 宽度优先打印

    https://www.nowcoder.net/practice/7fe2212963db4790b57431d9ed259701?tpId=13&tqId=11175&tPage= ...

  5. SQLite数据库基本操作

    SQLite 是一个开源的嵌入式关系数据库,实现自包容.零配置.支持事务的SQL数据库引擎. 其特点是高度便携.使用方便.结构紧凑.高效.可靠. 与其他数据库管理系统不同,SQLite 的安装和运行非 ...

  6. 使用 BenchmarkDotnet 测试代码性能 【Win10】单元测试中捕获异步方法的指定异常

    先来点题外话,清明节前把工作辞了(去 tm 的垃圾团队,各种拉帮结派.勾心斗角).这次找工作就得慢慢找了,不能急了,希望能找到个好团队,好岗位吧.顺便这段时间也算是比较闲,也能学习一下和填掉手上的坑. ...

  7. react 开发过程中的总结/归纳

    1.点击元素,获取绑定该事件的父级元素,使用 e.currentTarget.e.target 获取的是,出发该事件的元素,该元素有可能是所绑定事件的元素的子元素. 2.使用 react router ...

  8. DBCP,C3P0,Tomcat_JDBC 性能及稳定性測试

    原创文章,转载请指明出处:http://aub.iteye.com/blog/1404219, 尊重他人即尊重自己 DBCP,C3P0,Tomcat_JDBC 性能及稳定性測试 1.測试环境: 硬件环 ...

  9. HDU 4445 数学-抛物运动

                                                          D - Crazy Tank                                 ...

  10. 洛谷 P1759 通天之潜水

    P1759 通天之潜水 19通过 65提交 题目提供者lych 标签动态规划洛谷原创 难度普及/提高- 提交该题 讨论 题解 记录 最新讨论 暂时没有讨论 题目背景 直达通天路·小A历险记第三篇 题目 ...