您的位置:首页 > 其它

PAT 天梯赛 L1-048. 矩阵A乘以B 【数学】

2018-03-08 10:41 316 查看
题目链接

https://www.patest.cn/contests/gplt/L1-048

题意

给出两个矩阵,先判断两个矩阵能不能相乘,如果可以,就输出相乘 结果,如果不行 则按格式输出error

思路

先判断 第一个矩阵的列数 和第二个矩阵的行数 是否相等,如果相等就输出相乘结果,如果不相等 按格式输出ERROE

AC代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <cstdlib>
#include <ctype.h>
#include <numeric>
#include <sstream>
using namespace std;

typedef long long LL;
const double PI  = 3.14159265358979323846264338327;
const double E   = 2.718281828459;
const double eps = 1e-6;
const int MAXN   = 0x3f3f3f3f;
const int MINN   = 0xc0c0c0c0;
const int maxn   = 1e2 + 5;
const int MOD    = 1e9 + 7;
int a[maxn][maxn], b[maxn][maxn], c[maxn][maxn];

in
4000
t main()
{
int x1, y1;
int x2, y2;
cin >> x1 >> y1;
int i, j, k;
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(c, 0, sizeof(c));
for (i = 0; i < x1; i++)
{
for (j = 0; j < y1; j++)
{
scanf("%d", &a[i][j]);
}
}
cin >> x2 >> y2;
for (i = 0; i < x2; i++)
{
for (j = 0; j < y2; j++)
{
scanf("%d", &b[i][j]);
}
}
if (y1 != x2)
{
printf("Error: %d != %d\n", y1, x2);
}
else
{
printf("%d %d\n", x1, y2);
int num = 0;
for (i = 0; i < x1; i++)
{
for (j = 0; j < y2 ; j++)
{
if (j)
printf(" ");
num = 0;
for (k = 0; k < x2; k++)
{
num += a[i][k] * b[k][j];
}
cout << num;
}
cout << endl;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: