• 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛-K-Matrix Multiplication(矩阵乘法)


    题目描述

    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.
    示例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代码:
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn=30;
     4 struct Matrix
     5 {
     6     int m[maxn][maxn];
     7 }init1,init2,c;
     8 int t,row1,col1,row2,col2;
     9 void mul(Matrix a,Matrix b){
    10     for(int i=0;i<row1;i++){//枚举第一个矩阵的行。
    11         for(int j=0;j<col2;j++){//枚举第二个矩阵的列。
    12             c.m[i][j]=0;//注意清0
    13             for(int k=0;k<col1;k++)//枚举个数
    14                 c.m[i][j]+=a.m[i][k]*b.m[k][j];
    15         }
    16     }
    17 }
    18 int main(){
    19     cin>>t;
    20     for(int cas=1;cas<=t;++cas){
    21         cin>>row1>>col1>>row2>>col2;
    22         for(int i=0;i<row1;++i)
    23             for(int j=0;j<col1;++j)
    24                 cin>>init1.m[i][j];
    25         for(int i=0;i<row2;++i)
    26             for(int j=0;j<col2;++j)
    27                 cin>>init2.m[i][j];
    28         printf("Case %d:
    ",cas);
    29         if(col1!=row2)cout<<"ERROR"<<endl;
    30         else{
    31             mul(init1,init2);
    32             for(int i=0;i<row1;++i)
    33                 for(int j=0;j<col2;++j)
    34                     cout<<c.m[i][j]<<(j==col2-1?'
    ':' ');
    35         }
    36     }
    37     return 0;
    38 }
  • 相关阅读:
    table问题
    生成跟相应qq聊天
    查数组 indexOf()用法
    Ajax请求数据的两种方式
    Ajax面试题
    内部类及静态内部类的实例化
    为什么在开发中大部分的时候都在用session而Application基本上都不去使用?
    Java序列化与反序列化
    Java反射机制
    Java中线程同步的方法
  • 原文地址:https://www.cnblogs.com/acgoto/p/9452317.html
Copyright © 2020-2023  润新知