문제 링크입니다 https://programmers.co.kr/learn/courses/30/lessons/12940
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
두 수를 입력받아 두 수의 최대공약수와 최소공배수를 반환하는 함수를 완성하는 문제였습니다. 유클리드 호제법을 사용해 최대공약수를 구하고, 구한 최대공약수를 이용해 최소공배수를 구하면 되는 문제였습니다.
[소스코드]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <vector> | |
using namespace std; | |
vector<int> solution(int n, int m) { | |
vector<int> answer; | |
int a = n, b = m; | |
while(1){ | |
int r = a % b; | |
if(!r) { | |
answer.push_back(b); | |
break; | |
} | |
a = b, b = r; | |
} | |
answer.push_back(b*(n/b)*(m/b)); | |
return answer; | |
} |

'알고리즘 > Programmers' 카테고리의 다른 글
프로그래머스 LEVEL 1 : 정수 제곱근 판별 (0) | 2020.03.16 |
---|---|
프로그래머스 LEVEL 1 : 제일 작은 수 제거하기 (0) | 2020.03.15 |
프로그래머스 LEVEL 1 : 하샤드 수 (0) | 2020.03.15 |
프로그래머스 LEVEL 1 : 행렬의 덧셈 (0) | 2020.03.15 |
프로그래머스 LEVEL 1 : 예산 (0) | 2020.03.15 |