두 수를 입력받아서 두 수의 최대공약수와 최소공배수를 구하는 문제이다.

 

#include <string>
#include <vector>

using namespace std;

vector<int> solution(int n, int m) {
    vector<int> answer;
    
    int chk = 0;
    int a = n;
    int b = m;
    
    // 유클리드 호제법
    while(b != 0)
    {
        chk = a % b;
        a = b;
        b = chk;
    }
    answer.push_back(a);
    
    // 최소공배수를 구해서 저장
    answer.push_back(n*m/a);
    
    return answer;
}

+ Recent posts