[BOJ/C++] 1934 최소 공배수 구하기

GamzaTori·2024년 9월 20일

Algorithm

목록 보기
49/133

유클리드 호제법을 이용해 문제를 해결할 수 있습니다.

a와 b의 최소공배수는 ab/gcda*b/gcd로 나타낼 수 있습니다.

#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <stack>
#include <deque>
#include <string>

using namespace std;

int gcd(int a, int b);

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int N;
    cin >> N;

    for(int i=0; i<N; i++)
    {
        int a, b;
        cin >> a >> b;
        int result = a * b / gcd(a, b);
        cout << result << '\n';
    }

    return 0;
}

int gcd(int a, int b)
{
    if (b == 0)
        return a;
    else
        return gcd(b, a % b);
}
profile
게임 개발 공부중입니다.

0개의 댓글