牛客网 Matrix Multiplication(数学)

链接:https://www.nowcoder.com/acm/contest/163/K
来源:牛客网
题目描述
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 A 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.
这里写图片描述
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.
示例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
矩阵的乘法计算

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,k=0;cin>>t;
    while(t--)
    {
        k++;
        int n1,m1,n2,m2;
        int a[25][25],b[25][25],c[25][25];
        cin>>n1>>m1>>n2>>m2;
        for(int i=0;i<n1;i++)
            for(int j=0;j<m1;j++)
                cin>>a[i][j];
        for(int i=0;i<n2;i++)
            for(int j=0;j<m2;j++)
                cin>>b[i][j];
        printf("Case %d:\n",k);
        if(m1!=n2) cout<<"ERROR"<<endl;
        else{
            memset(c,0,sizeof(c));
            for(int i=0;i<n1;i++){
                for(int j=0;j<m2;j++){
                    for(int x=0;x<m1;x++)
                    c[i][j]+=a[i][x]*b[x][j];
                }
            }
            for(int i=0;i<n1;i++){
                for(int j=0;j<m2;j++){
                    cout<<c[i][j]<<" ";
                }
                cout<<endl;
            }
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值