C# 알고리즘 코드카타

81. N개의 최소공배수

잼잼재미 2024. 2. 27. 09:32

using System.Linq;

public class Solution {
    public int solution(int[] arr) {
        int answer = 0;
        int max;
        int count = 0;
        bool clear;
        
        max = arr.Max();
        
        while(true)
        {
            clear = true;
            count++;
            answer = max * count;
            
            for(int i = 0; i < arr.Length; ++i)
            {
                if(answer % arr[i] != 0)
                {
                    clear = false;
                    break;
                }
            }
            
            if(clear) break;
        }
        
        return answer;
    }
}

 

using System.Linq로 배열에서 가장 큰 값을 구함. 그 값의 배수에서 다른 모든 배열을 나누었을 때, 0인지 판별해서 최소 공배수를 구함.

'C# 알고리즘 코드카타' 카테고리의 다른 글

83. 귤 고르기  (0) 2024.03.05
82. 멀리 뛰기  (0) 2024.02.29
80. 예상 대진표  (0) 2024.02.23
79. 카펫  (0) 2024.02.21
78. 피보나치 수  (0) 2024.02.21