행렬의 곱셈

[문제 Link] https://programmers.co.kr/learn/courses/30/lessons/12949

문제 설명


2차원 행렬 arr1과 arr2를 입력받아, arr1에 arr2를 곱한 결과를 반환하는 함수, solution을 완성해주세요.


제한사항


  • 행렬 arr1, arr2의 행과 열의 길이는 2 이상 100 이하입니다.
  • 행렬 arr1, arr2의 원소는 -10 이상 20 이하인 자연수입니다.
  • 곱할 수 있는 배열만 주어집니다.


입출력 예


arr1 arr2 return
[[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]]


입출력 예 설명



로직


전체 코드


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

class Solution
{
    public static int[,] solution(int[,] arr1, int[,] arr2) {
        int[,] answer = new int[arr1.GetLength(0),arr2.GetLength(1)];

        for(int i =0;i<arr1.GetLength(0);i++)
        {
            for(int j =0;j<arr2.GetLength(1);j++)
            {
                int temp =0;
                while(temp<arr2.GetLength(0))
                {
                    answer[i,j] += arr1[i,temp] * arr2[temp,j];
                    temp++;
                }
            }
        }

        return answer;
    }

    public static void Main(string[] args)
    {
        int[,] arr1 = new int[,]{{1, 4}, {3, 2}, {4, 1}};
        int[,] arr2 = new int[,]{{3, 3}, {3, 3}};
        int[,] results = solution(arr1,arr2);
        for(int i =0;i<results.GetLength(0);i++)
        {
            for(int j =0;j<results.GetLength(1);j++)
            {
                Console.Write(results[i,j]+ " ");
            }
            Console.WriteLine();
        }
    }
}


결과