GitHubSeob

C++ / 프로그래머스 / N개의 최소공배수 본문

Programmers/Level 2

C++ / 프로그래머스 / N개의 최소공배수

GitHubSeob 2023. 7. 6.
문제

 

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

 

프로그래머스

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

programmers.co.kr

문제풀이

 

유클리드 호제법으로 최대공약수(gcd)를 구하고 최대공약수를 이용해 최소공배수(lcm)를 구할 수도 있다.

대신에 더 간편한 STL을 이용해 최소공배수를 구했다.

 

반복문을 이용해 N개의 최소공배수를 구할 수 있다.

코드
#include <string>
#include <numeric>
#include <vector>

using namespace std;

int solution(vector<int> arr) {
    int answer(1), idx(0);

    for (idx = 0; idx < arr.size(); ++idx) {
        answer = lcm(answer, arr[idx]);
    }
    return answer;
}