2018上海大都会赛-K Matrix Multiplication (矩阵乘法模板)

题目链接 https://www.nowcoder.com/acm/contest/163/K

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MOD = 1e9+7;
const int INF = 1000000000;
const int maxn = 25;
int A[maxn][maxn];
int B[maxn][maxn];
int C[maxn][maxn];
int T,m,n,p,q;

int main() {
	cin >> T;
	for (int kase = 1; kase <= T; kase++) {
		cin >> m >> n >> p >> q;
		memset(A,0,sizeof(A));
		memset(B,0,sizeof(B));
		memset(C,0,sizeof(C));
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				cin >> A[i][j];
			}
		}
		for (int i = 0; i < p; i++) {
			for (int j = 0; j < q; j++) {
				cin >> B[i][j];
			}
		}
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < q; j++) {
				for (int k = 0; k <= n; k++) {
					C[i][j] += A[i][k] * B[k][j];
				}
			}
		}
		cout << "Case " << kase <<":" << endl; 
		if (n != p) {
			puts("ERROR");
			continue;
		} else {
			for (int i = 0; i < m; i++) {
				for (int j = 0; j < q; j++) {
					cout << C[i][j] << " ";
				}
				cout << endl; 
			}
		}
	} 
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Eimhin_Tang/article/details/81430570