L1-048 矩阵A乘以B

  • Post author:
  • Post category:其他



PTA | 程序设计类实验辅助教学平台

线性代数的矩阵相乘,当时想用一个数组储存,这反而复杂了

直接输出,相乘的时候

#include <iostream>
using namespace std;
int a[110][110], b[110][110];
int m, n, x, y;

int main() {
	cin >> m >> n;
	for (int i = 0; i < m; i++)
		for (int j = 0; j < n; j++)
			cin >> a[i][j];

	cin >> x >> y;
	for (int i = 0; i < x; i++)
		for (int j = 0; j < y; j++)
			cin >> b[i][j];

	if (n != x) {
		printf("Error: %d != %d\n", n, x);
	} else {
		printf("%d %d\n", m, y);
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < y; j++) {
				int sum = 0;
				for (int k = 0; k < n; k++)
					sum += a[i][k] * b[k][j];
				if (j < y - 1)
					printf("%d ", sum);
				else
					printf("%d\n", sum);
			}
		}
	}
	return 0;
}



版权声明:本文为EnochChen_原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。