[프로그래머스] 최대공약수와 최소공배수

Zoo Da·2021년 10월 8일
0

프로그래머스

목록 보기
6/10
post-thumbnail

링크

https://programmers.co.kr/learn/courses/30/lessons/12940

sol1) 수학

#include <string>
#include <vector>

using namespace std;

int gcd(int a,int b) {return (b ? gcd(b, a % b) : a);}
int lcm(int a, int b) {
	return a * b / gcd(a, b);
}

vector<int> solution(int n, int m) {
    vector<int> answer;
    answer.push_back(gcd(n,m));
    answer.push_back(lcm(n,m));
    return answer;
}

유클리드 호제법을 이용해서 구해주면 됩니다.

profile
메모장 겸 블로그

0개의 댓글