题目描述

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. 【BZOJ2818】Gcd(莫比乌斯反演,欧拉函数)

    题意:给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对 1<=N<=10^7 思路:莫比乌斯反演,同BZOJ2820…… ; ..max]of ...

  2. NOIP 2010 机器翻译

    P1540 机器翻译 题目背景 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 题目描述 这个翻译软件的原理很简单,它只是从头到尾,依次将每个英文单词用对应的中文含义来替换.对于 ...

  3. MyBatis实体属性与表的字段不对应的解决方案

    1.通过在查询的SQL语句中定义字段名的别名,让字段名的别名和实体类的属性名一致,这样就可以表的字段名和实体类的属性名一一对应上,这种方式是通过在SQL语句中定义别名来解决字段名和属性名的映射关系的. ...

  4. JSTL-格式标签库

    主页:http://www.cnblogs.com/EasonJim/p/6958992.html的分支页. 一.<fmt:formatNumber> <fmt:formatNumb ...

  5. Canon iP2780/iP2788 清零软件

    http://www.drvsky.com/driver/iP2780_Tools.htm http://www.dyjqd.com/soft/6085.html#download http://v. ...

  6. Cardboard虚拟现实开发初步(二)

    Google Cardboard 虚拟现实眼镜开发初步(二) Cardboard SDK for Unity的使用 上一篇文章作为系列的开篇,主要是讲了一些虚拟现实的技术和原理,本篇就会带领大家去看一 ...

  7. python爬虫实践--求职Top10城市

    前言 从智联招聘爬取相关信息后,我们关心的是如何对内容进行分析,获取用用的信息.本次以上篇文章“5分钟掌握智联招聘网站爬取并保存到MongoDB数据库”中爬取的数据为基础,分析关键词为“python” ...

  8. windows server 证书的颁发与IIS证书的使用 Dapper入门使用,代替你的DbSQLhelper Asp.Net MVC中Action跳转(转载)

    windows server 证书的颁发与IIS证书的使用   最近工作业务要是用服务器证书验证,在这里记录下一. 1.添加服务器角色 [证书服务] 2.一路下一步直到证书服务安装完成; 3.选择圈选 ...

  9. 关于ExecuteNonQuery执行存储过程的返回值 、、实例讲解存储过程的返回值与传出参数、、、C#获取存储过程的 Return返回值和Output输出参数值

    关于ExecuteNonQuery执行存储过程的返回值 用到过ExecuteNonQuery()函数的朋友们在开发的时候肯定这么用过. if(cmd.ExecuteNonQuery("xxx ...

  10. 控件CListCtr详解

    1.CListCtrl控件 CListCtrl控件在数据库编程中是用得比较多的控件之一,也是Window控件中较难掌握的一个控件.他可以有四显示方式,Report.List.Icon.SmallIco ...