두 수를 입력받아서 두 수의 최대공약수와 최소공배수를 구하는 문제이다.
#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;
}
'개발 > 코딩테스트' 카테고리의 다른 글
[프로그래머스] 행렬의 덧셈 (0) | 2021.01.16 |
---|---|
[프로그래머스] 콜라츠 추측 (0) | 2021.01.16 |
[프로그래머스] 제일 작은 수 제거하기 (0) | 2021.01.09 |
[프로그래머스] 정수 제곱근 판별 (0) | 2021.01.08 |
[프로그래머스] 평균 구하기 (0) | 2021.01.08 |