티스토리 뷰

https://programmers.co.kr/learn/courses/30/lessons/12949

 

코딩테스트 연습 - 행렬의 곱셈

[[2, 3, 2], [4, 2, 4], [3, 1, 4]] [[5, 4, 3], [2, 4, 1], [3, 1, 1]] [[22, 22, 11], [36, 28, 18], [29, 20, 14]]

programmers.co.kr

 

[프로그래머스][C++] 행렬의 곱셈

#include <string>
#include <vector>

using namespace std;

vector<vector<int>> solution(vector<vector<int>> arr1, vector<vector<int>> arr2) {
	int r1 = arr1.size();
	int r2 = arr2.size();	// == c1
	int c2 = arr2[0].size();

	vector<vector<int>> answer(r1, vector<int>(c2, 0));

	for (int i = 0; i < r1; i++) {
		for (int j = 0; j < c2; j++) {
			int sum = 0;
			for (int k = 0; k < r2; k++) {
				sum += arr1[i][k] * arr2[k][j];
			}
			answer[i][j] = sum;
		}
	}

	return answer;
}
댓글