【天梯赛】L1-048. 矩阵A乘以B
2018-03-20 18:36
281 查看
给定两个矩阵A和B,要求你计算它们的乘积矩阵AB。需要注意的是,只有规模匹配的矩阵才可以相乘。即若A有Ra行、Ca列,B有Rb行、Cb列,则只有Ca与Rb相等时,两个矩阵才能相乘。
输入格式:
输入先后给出两个矩阵A和B。对于每个矩阵,首先在一行中给出其行数R和列数C,随后R行,每行给出C个整数,以1个空格分隔,且行首尾没有多余的空格。输入保证两个矩阵的R和C都是正数,并且所有整数的绝对值不超过100。
输出格式:
若输入的两个矩阵的规模是匹配的,则按照输入的格式输出乘积矩阵AB,否则输出“Error: Ca != Rb”,其中Ca是A的列数,Rb是B的行数。
输入样例1:
2 3
1 2 3
4 5 6
3 4
7 8 9 0
-1 -2 -3 -4
5 6 7 8
输出样例1:
2 4
20 22 24 16
53 58 63 28
输入样例2:
3 2
38 26
43 -5
0 17
3 2
-11 57
99 68
81 72
输出样例2:
Error: 2 != 3
输入格式:
输入先后给出两个矩阵A和B。对于每个矩阵,首先在一行中给出其行数R和列数C,随后R行,每行给出C个整数,以1个空格分隔,且行首尾没有多余的空格。输入保证两个矩阵的R和C都是正数,并且所有整数的绝对值不超过100。
输出格式:
若输入的两个矩阵的规模是匹配的,则按照输入的格式输出乘积矩阵AB,否则输出“Error: Ca != Rb”,其中Ca是A的列数,Rb是B的行数。
输入样例1:
2 3
1 2 3
4 5 6
3 4
7 8 9 0
-1 -2 -3 -4
5 6 7 8
输出样例1:
2 4
20 22 24 16
53 58 63 28
输入样例2:
3 2
38 26
43 -5
0 17
3 2
-11 57
99 68
81 72
输出样例2:
Error: 2 != 3
#include <bits/stdc++.h> using namespace std; #define mem(a,n) memset(a,n,sizeof(a)) #define memc(a,b) memcpy(a,b,sizeof(b)) #define rep(i,a,n) for(int i=a;i<n;i++) ///[a,n) #define pb push_back #define IO ios::sync_with_stdio(false) #define fre freopen("in.txt","r",stdin) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 typedef long long ll; typedef unsigned long long ull; const double PI=acos(-1.0); const double E=2.718281828459045; const double eps=1e-8; const int INF=0x3f3f3f3f; const int MOD=1e8+7; const int N=5e2+5; const ll maxn=1e6+5; const int dir[4][2]= {-1,0,1,0,0,-1,0,1}; const ll inf=0x3f3f3f3f3f3f3f3f; typedef vector<ll>vec; typedef vector<vec>mat; mat mul(mat &A,mat &B) { mat C(A.size(),vec(B[0].size())); for(int i=0; i<A.size(); i++) for(int k=0; k<B.size(); k++) for(int j=0; j<B[0].size(); j++) C[i][j]=(C[i][j]+A[i][k]*B[k][j]); return C; } void input(int m,int n,mat &a) { rep(i,0,m) rep(j,0,n) scanf("%d",&a[i][j]); } int main() { int m,s,l,n; while(~scanf("%d%d",&m,&s)) { mat A(m,vec(s)); input(m,s,A); scanf("%d%d",&l,&n); mat B(l,vec(n)); input(l,n,B); if(s!=l) { printf("Error: %d != %d\n",s,l); continue; } printf("%d %d\n",m,n); mat ans(m,vec(n)); ans=mul(A,B); rep(i,0,m) { rep(j,0,n) if(j!=n-1) printf("%d ",ans[i][j]); else printf("%d\n",ans[i][j]); } } return 0; }
相关文章推荐
- 天梯赛L1-048矩阵A乘以B
- PAT 天梯赛 L1-048. 矩阵A乘以B 【数学】
- PAT - 天梯赛 L1-048 矩阵A乘以B
- 团体程序设计天梯赛-练习集 L1-048. 矩阵A乘以B
- 天梯赛习题 L1-048. 矩阵A乘以B
- L1-048. 矩阵A乘以B
- L1-048. 矩阵A乘以B
- C++ L1-048. 矩阵A乘以B
- L1-048. 矩阵A乘以B
- L1-048. 矩阵A乘以B
- L1-048. 矩阵A乘以B
- L1-048. 矩阵A乘以B
- L1-048. 矩阵A乘以B
- L1-048. 矩阵A乘以B
- L1-048. 矩阵A乘以B
- L1-048. 矩阵A乘以B
- L1-048 矩阵A乘以B(矩阵乘法运算)
- L1-048. 矩阵A乘以B
- L1-048. 矩阵A乘以B
- L1-048. 矩阵A乘以B