seunghyun Note

[프로그래머스] N개의 최소공배수 with JAVA 본문

코딩테스트/백준

[프로그래머스] N개의 최소공배수 with JAVA

승숭슝현 2024. 1. 4. 10:14

https://programmers.co.kr/learn/courses/30/lessons/12953

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제 풀이

💻 GCD 재귀 돌리기, 유클리드 호제법을 통한 문제 해결

 

class Solution {
    public int solution(int[] arr) {
        int answer= arr[0];
        for(int i=1; i<arr.length;i++){
            answer = lcm(answer,arr[i]);
            System.out.println(answer);
        }
        return answer;
    }
 private static int gcd(int a, int b) {
        if (a % b == 0) {
            return b;
        }
        return gcd(b, a % b);
    }
 
        int lcm(int a, int b){
            return a*b/gcd(a,b);
        }
 
}

 

728x90