빠켱이

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

알고리즘/프로그래머스 알고리즘

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

빠켱이 2021. 5. 5. 14:29
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int gcd(int a, int b){
    int r = 1;
    while(r > 0){
        r = b % a;
        b = a;
        a = r;
    }
    return b;
}
int solution(vector<int> arr) {
    int answer = arr[0];
    sort(arr.begin(), arr.end());
    for(int i = 1; i < arr.size(); i++){
        int tmp = gcd(answer, arr[i]);
        answer = answer * arr[i] / tmp;
    }
    return answer;
}
Comments