标签:枚举 down tps width hat isp inpu file 一个
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
解题思路:题意描述得很清楚,矩阵乘法,直接套公式即可。
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:\n",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?‘\n‘:‘ ‘); 35 } 36 } 37 return 0; 38 }
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛-K-Matrix Multiplication(矩阵乘法)
标签:枚举 down tps width hat isp inpu file 一个
原文地址:https://www.cnblogs.com/acgoto/p/9452317.html