웅재의 코딩세상

[프로그래머스] 최대공약수와 최소공배수 본문

코딩테스트/프로그래머스 - LV 1

[프로그래머스] 최대공약수와 최소공배수

웅드 2024. 2. 9. 17:13

 

  • 풀이
#include <string>
#include <vector>

using namespace std;
int gcd(int a, int b);

vector<int> solution(int n, int m) {
    vector<int> answer;
    int tmp=0, g=0, lcm=0;
    if(n>m){
        tmp = n;
        n = m;
        m = tmp;
    }
    g = gcd(n,m);
    lcm = m*n / g;
    answer.push_back(g);
    answer.push_back(lcm);
    
    return answer;
}

int gcd(int a, int b){
    int c;
    while(b !=0){
        c = a%b;
        a=b;
        b=c;
    }
    return a;
}
반응형