[백준] 1934번 최소공배수 / C++

#문제

백준 1934번 최소공배수

#풀이

#include <iostream>
#include <vector>

using namespace std;

vector<int> ans;

void LCM(const int x, const int y)
{
	int a = x > y ? x : y;
	int b = x > y ? y : x;

	int r = a % b;

	while (r != 0)
	{
		a = b;
		b = r;
		r = a % b;
	}

	ans.push_back(x * y / b);
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(NULL);

	int T;
	cin >> T;

	for (int i = 0; i < T; ++i)
	{
		int a, b;
		cin >> a >> b;

		LCM(a, b);
	}

	for (int i = 0; i < ans.size(); ++i)
	{
		cout << ans[i] << '\n';
	}

	return 0;
}

#정리

유클리드 알고리즘을 사용하여 최대공약수를 구하고, 두 수의 곱을 최대공약수로 나눠 최소공배수를 구했다.




    Enjoy Reading This Article?

    Here are some more articles you might like to read next:

  • [백준] 9613번 GCD 합 / C++
  • [백준] 1940번 주몽 / C++
  • [백준] 1010번 다리 놓기 / C++
  • [백준] 1037번 최소공배수 / C++
  • [백준] 1269번 대칭 차집합 / C++