반응형
Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 조합
- 재귀함수
- 백준 알고리즘
- 프로그래머스
- 네트워크
- 너비탐색
- IT
- 언더라이터
- 데이터베이스
- backjoon
- 자바
- 익명객체
- CKLU
- 다이나믹 프로그래밍
- 개발
- 익명클래스
- 알고리즘
- 삼성SW테스트
- dp
- 멀티스레드
- Java
- 안드로이드
- dfs
- 백준
- 금융IT
- BFS
- 모바일
- 삼성sw문제
- 현대오토에버 코딩테스트
- Android
Archives
- Today
- Total
Limky 삽질블로그
[Algorithm] 행렬의 곱셈 (ProductMatrix) 본문
반응형
Programmers Level_02
행렬의 곱셈 (ProductMatrix)
행렬의 곱셈은, 곱하려는 두 행렬의 어떤 행과 열을 기준으로, 좌측의 행렬은 해당되는 행, 우측의 행렬은 해당되는 열을 순서대로 곱한 값을 더한 값이 들어갑니다. 행렬을 곱하기 위해선 좌측 행렬의 열의 개수와 우측 행렬의 행의 개수가 같아야 합니다. 곱할 수 있는 두 행렬 A,B가 주어질 때, 행렬을 곱한 값을 출력하는 productMatrix 함수를 완성해 보세요.
package Programmers_Level02;
class ProductMatrix {
public static void main(String[] args) {
ProductMatrix c = new ProductMatrix();
int[][] a = { { 1, 2 , 3}, { 2, 3 ,4} };
int[][] b = { { 3, 4 }, { 5, 6 },{ 3, 4 } };
// 아래는 테스트로 출력해 보기 위한 코드입니다.
System.out.println("행렬의 곱셈 : " + c.productMatrix(a, b));
}
public int[][] productMatrix(int[][] A, int[][] B) {
int[][] answer = new int[A.length][B[0].length];
for(int i=0; i<answer.length;i++){
for(int j=0; j<answer[0].length;j++){
for(int k=0; k<A[0].length;k++){
answer[i][j] += A[i][k]*B[k][j];
}
}
}
// for (int i = 0; i < answer.length; i++) {
// for (int j = 0; j < answer[i].length; j++) {
// System.out.print(answer[i][j] + " "); // 열 출력
// }
// System.out.println(); // 행 출력
// }
return answer;
}
}
반응형
'Algorithm > Programmers' 카테고리의 다른 글
| [Algorithm] 시저암호 (Caesar) (0) | 2017.10.05 |
|---|---|
| [Algorithm] 정수 내림차순으로 배치하기 (0) | 2017.10.05 |
| [Algorithm] 소수 찾기 (NumberOfPrime ) (0) | 2017.10.04 |
| [Algorithm] 하샤드(Harshad) 수 (0) | 2017.10.04 |
| [Algorithm] 2016년 (0) | 2017.10.04 |