프로그래밍

프로그래머스 C# N개의 최소공배수(메소드 활용, 유클리드 호제법, m대각선) n-m-temp

노마드선샤인 2023. 1. 24. 18:03
728x90

public class Solution {
        public int LCM(int a,int b)
        {
            int n = a;
            int m = b;
            int temp = 0;
            
        while(m > 0)
        {
            temp = n%m;
            n = m;
            m = temp;
        }
        return (a*b)/n;
    }
        
    public int solution(int[] arr) 
    {
        int answer = LCM(arr[0],arr[1]);
        
        for(int i=2; i<arr.Length; i++)
        {
            answer = LCM(answer,arr[i]);
        }
        return answer;
    }
}

728x90