백준 algorithm

백준 2609 - 최대공약수와 최소공배수

cosmohoo 2020. 3. 13. 13:03
반응형

문제 설명

 

 

 

 

=> GCD와 LCM ( 최대공약수와 최소공배수 ) 를 구할 수 있는지에 대한 문제이다. 

=> 함수 두개를 생성하여 만들었다. 

=> GCD : 유클리드 호제법  LCM :gcd*(A/gcd)*(B/gcd) 이다. 

 

 

 

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

int GCD (int A, int B)
{
    while(B!=0)
    {
        int tmp=B;
        B=A%B;
        A=tmp;
    }
    return A;
}

int LCD(int A, int B)
{
    int gcd=GCD(A, B);
    return gcd*(A/gcd)*(B/gcd);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int A, B;
    cin>> A>>B;
    
    cout<<GCD(A,B)<<'\n';
    cout <<LCD(A,B)<<'\n';
    
    return 0;
}

반응형