행렬의 덧셈

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

문제 설명


행렬의 덧셈은 행과 열의 크기가 같은 두 행렬의 같은 행, 같은 열의 값을 서로 더한 결과가 됩니다. 2개의 행렬 arr1과 arr2를 입력받아, 행렬 덧셈의 결과를 반환하는 함수, solution을 완성해주세요.


제한사항


  • 행렬 arr1, arr2의 행과 열의 길이는 500을 넘지 않습니다.


입출력 예


arr1 arr2 return
[[1,2],[2,3]] [[3,4],[5,6]] [[4,6],[7,9]]
[[1],[2]] [[3],[4]] [[4],[6]]


입출력 예 설명



제한시간 안내


null

전체 코드


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),arr1.GetLength(1)];
        for(int i =0;i<arr1.GetLength(0);i++)
            for(int j =0;j<arr1.GetLength(1);j++)
                answer[i,j] = arr1[i,j]+arr2[i,j];
        return answer;
    }

    public static void Main(string[] args)
    {
        int[,] arr1 = new int[,]{{1,2},{2,3}};
        int[,] arr2 = new int[,]{{3,4},{5,6}};
        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();
        }
    }
}


결과