N개의 최소 공배수

JJW·2024년 12월 16일

코딩 테스트

목록 보기
19/23

문제

--

문제 풀이

public class Solution 
{
    // 최대 공약수
    public int GCD(int a, int b)
    {
        while(b != 0)
        {
            int temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }
    
    // 최소 공배수
    public int LCM(int a, int b)
    {
        return a * b / GCD(a,b);
    }
    
    public int solution(int[] arr) 
    {
        int answer = arr[0];
        
        for(int i = 0; i < arr.Length; i++)
        {
            answer = LCM(answer,arr[i]);
        }
        return answer;
    }
}
profile
Unity 게임 개발자를 준비하는 취업준비생입니다..

0개의 댓글