辗转相除法求GCD。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long int ll;
ll GCD(ll x, ll y)
{
return !y ? x : GCD(y, x % y);
}
ll LCM(ll x, ll y)
{
return x * y / GCD(x, y);
}
int main()
{
ll a, b;
while (cin >> a >> b)
{
cout << GCD(a, b) << ' ' << LCM(a, b) << endl;
}
return 0;
}
本文介绍了一种使用辗转相除法求最大公约数(GCD)及最小公倍数(LCM)的C++实现。通过递归方式实现了GCD的计算,并基于此计算了两个数的LCM。
1007

被折叠的 条评论
为什么被折叠?



