zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    listview item 动画
    android sqlite blob
    Python3 配置文件(configparser)(转载)
    python之字符串格式化(format)
    PHP模拟发送POST请求之一、HTTP协议头部解析
    用HTML/JS/PHP方式实现页面延时跳转
    用memoization优化递归算法[JS/PHP实现]
    开通博客,记录一下。
    SpringMvc Json LocalDateTime 互转,form urlencoded @ModelAttribute 转换
    Springdata mongodb 版本兼容 引起 Error [The 'cursor' option is required, except for aggregate with the explain argument
  • 原文地址:https://www.cnblogs.com/acgoto/p/9452317.html
Copyright © 2011-2022 走看看