题目描述

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 ;
}

最新文章

  1. IP地址,子网掩码,默认网关,DNS服务器知识详解(转)
  2. 用jsonp实现搜索框功能
  3. Java经典实例:实现一个简单堆栈
  4. HBase 学习笔记
  5. NetworkComms V3 之同步收发数据
  6. js设置输入框失去光标与光标选中时样式
  7. centos 5.8 64位系统安装 mysql5.6
  8. C Primer
  9. C# IO操作(三)文件编码
  10. 2015第10周日CSS—3
  11. win10 uwp 分治法
  12. 浅谈WPF中的MVVM框架--MVVMFoundation
  13. python之全局变量与局部变量
  14. Matlib
  15. 【CSS 技能提升】 :before和:after的使用
  16. linux 修改时间和时区
  17. kolla-ansible源码分析
  18. Activiti的25张表
  19. K邻近分类算法
  20. Python yield的用法

热门文章

  1. 对付 MySQL 的死连接,Sleep的进程的来源探究[转]
  2. ***CSS总结-原创
  3. Delphi DBGrid实现多选
  4. codevs 3498 小木棍
  5. Redis Cluster集群搭建后,客户端的连接研究(Spring/Jedis)(待实践)
  6. ThinkPHP3.2 点击看不清刷新验证码
  7. Linux学习系列之MySQL备份
  8. C++学习之构造函数中的异常处理
  9. Mariadb 主从
  10. vs2012下安装Cocos2d-x模板问题