2023. 2. 21. 11:37ใ์ฝ๋ฉํ ์คํธ ์ฐ์ต/ํ๋ก๊ทธ๋๋จธ์ค_2023
๋ฌธ์ ์ค๋ช
2์ฐจ์ ํ๋ ฌ arr1๊ณผ arr2๋ฅผ ์ ๋ ฅ๋ฐ์, arr1์ arr2๋ฅผ ๊ณฑํ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋ ํจ์, solution์ ์์ฑํด์ฃผ์ธ์.
- ํ๋ ฌ arr1, arr2์ ํ๊ณผ ์ด์ ๊ธธ์ด๋ 2 ์ด์ 100 ์ดํ์ ๋๋ค.
- ํ๋ ฌ arr1, arr2์ ์์๋ -10 ์ด์ 20 ์ดํ์ธ ์์ฐ์์ ๋๋ค.
- ๊ณฑํ ์ ์๋ ๋ฐฐ์ด๋ง ์ฃผ์ด์ง๋๋ค.
[[1, 4], [3, 2], [4, 1]] | [[3, 3], [3, 3]] | [[15, 15], [15, 15], [15, 15]] |
[[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]] |
๋์ ํ์ด
public int[][] solution(int[][] arr1, int[][] arr2) {
int[][] answer = new int[arr1.length][arr2[0].length];
for (int i = 0; i < arr1.length; i++)
{
for (int j = 0; j < arr2[0].length; j++)
{
for (int k = 0; k < arr1[0].length; k++)
{
answer[i][j] += arr1[i][k] * arr2[k][j];
}
}
}
return answer;
}
์ฒซ๋ฒ์งธ ์์ ๋ฅผ ์๊ฐํด๋ณด๋ฉด
answer[0][0] = arr1[0][0] * arr2[0][0] + arr1[0][1] * arr2[1][0] , answer[0][1] = arr1[0][0] * arr2[0][1] + arr1[0][1] * arr2[1][1]
answer[1][0] = arr1[1][0] * arr2[0][0] + arr1[1][1] * arr2[1][0] , answer[1][1] = arr1[1][0] * arr2[0][1] + arr1[1][1] * arr2[1][1]
answer[2][0] = arr1[2][0] * arr2[0][0] + arr1[2][1] * arr2[1][0] , answer[2][1] = arr1[2][0] * arr2[0][1] + arr1[2][1] * arr2[1][1]
์ด๋ฅผ ๊ณต์ํ ํ๋ฉด
answer[i][j] += arr1[i][k] * arr2[k][j] ๋ก ์ผ์ค ๋ฐ๋ณต๋ฌธ์ ์ฌ์ฉํด์ ํ๋ ฌ์ ๊ณฑ์ ์ ๊ตฌํ ์ ์๋ค.