Gidhub BE Developer

[Programmers] 최대공약수와 최소공배수

2018-07-24
goodGid

Problem

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


[1] Answer Code (18. 07. 24)


// [1]
#define swap(a,b) a ^= b ^= a ^= b

vector<int> solution(int a,int b){
    if(a > b){
        swap(a,b);
    }
    
    vector<int> answer;
    for(int i = a; i > 0; i--){
        if(((a%i) == 0) && ((b%i) == 0)){
            answer.push_back(i);
            answer.push_back((a*b)/i);
            break;
        }
    }
    return answer;
}


// [2]
int gcd(int a, int b){ 
    return (a % b == 0 ? b : gcd(b,a%b));
}


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


[1] Answer Code (18. 07. 24)

  • [1] : GCD / LCM 신기하다.

Recommend

Index