题目描述

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. FZU Problem 2171 防守阵地 II (线段树区间更新模板题)

    http://acm.fzu.edu.cn/problem.php?pid=2171 成段增减,区间求和.add累加更新的次数. #include <iostream> #include ...

  2. 高数(A)下 第十一章

    11.1 11.2 11.3 11.4 11.5

  3. JSP操作

    以下内容引用自http://wiki.jikexueyuan.com/project/jsp/actions.html: JSP操作(Action)使用XML语法结构来控制Servlet引擎的行为.可 ...

  4. POJ 1384 POJ 1384 Piggy-Bank(全然背包)

    链接:http://poj.org/problem?id=1384 Piggy-Bank Time Limit: 1000MS Memory Limit: 10000K Total Submissio ...

  5. Python学习系列之内置函数

    数学相关 abs(a):求取绝对值 max(list):求取list最大值 min(list):求取list最小值 sum(list):求取list元素的和 sorted(list):排序,返回排序后 ...

  6. ubuntu10.04 建V

    ubuntu10.04架设vpn服 vpn 安装:  pptpd:apt-get install pptpd 1. 配置网络IP地址,编辑 vim /etc/pptpd.conf ,去掉下面两行前面# ...

  7. karaf增加自己定义log4j的配置

    配置文件: karaf_home/etc/org.ops4j.pax.logging.cfg 增加配置: ### direct log messages to stdout ### log4j.app ...

  8. 【转】React 常用面试题目与分析

    作者:王下邀月熊链接:https://zhuanlan.zhihu.com/p/24856035来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 本文有一定概率为水文,怕 ...

  9. H264--4--H264编码[7]

    ----------------------------------- 编码器输出格式 ---------------------------------- 总的来说H264的码流的打包方式有两种,一 ...

  10. android View页面布局总结 最全总结(转)

    下面是我在工作中总结的内容,希望对大家有帮助. 一.布局 View的几种布局显示方式有下面几种:线性布局(LinearLayout).相对布局(RelativeLayout).表格布局(TableLa ...