Gidhub BE Developer

[BOJ] 2609. 최대공약수와 최소공배수

2018-04-03
goodGid

Problem

Problem URL : 최대공약수와 최소공배수


[1] Answer Code (18. 04. 03)


#include<iostream>
using namespace std;

int funGCM(int a, int b){
    return (a % b == 0 ? b : funGCM(b,a%b));
}

int main(){
    int a,b;
    cin >> a >> b;
    int gcm = funGCM(a, b);
    int lcm = a * b / gcm;
    cout << gcm << endl;
    cout << lcm << endl;
    return 0;
}

Review

  • GCM & LCM 문제 !

[2] Answer Code (18. 09. 16)

#include<iostream>
using namespace std;

int main(){
    int a,b;
    cin >> a >> b;
    
    if( a < b){
        int tmp = b;
        b = a;
        a = tmp;
    }
    
    for(int i=a; i>0; i--){
        if( (a%i==0)
           && (b%i==0) ){
            cout << i << endl;
            cout << (a*b)/i << endl;
            break;
        }
    }

    return 0;
}

Review

  • for문으로 GCM & LCM 처리

Recommend

Index