관리 메뉴

Try

[Programmers/C++] N개의 최소공배수 본문

Algorithm/Programmers

[Programmers/C++] N개의 최소공배수

HAS3ONG 2019. 4. 17. 23:06
#include <string>
#include <vector>

using namespace std;

int GCD(int a, int b){
    int c;
    while(b != 0){
        c = a % b;
        a = b;
        b = c;
    }
    return a;
}
int LCM(int a, int b){
    return a * b / GCD(a, b);
}
int solution(vector<int> arr) {
    int answer = 0;
    int lcm = 1;
    for(int i = 0; i < arr.size(); i++){
        lcm = LCM(lcm, arr[i]);
    }
    answer = lcm;
    return answer;
}

 

 

출처

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

'Algorithm > Programmers' 카테고리의 다른 글

[Programmers/C++] 소수의 합  (0) 2019.04.30
[Programmers/C++] 피보나치 수  (0) 2019.04.17
[Programmers/C++] 탑  (0) 2019.04.16
[Programmers/C++] 약수의 합  (0) 2019.04.12
[Programmers/C++] 문자열 내 p와 y의 개수  (0) 2019.04.12
Comments