> 数学 >
求两个数的最大公约数和最小公倍数用c++
人气:484 ℃ 时间:2020-04-26 06:57:57
解答
是辗转法
代码:
long gcd(long x,long y)//最大公约数
{ // get the greatest common divisor of two integer(GCD)
long t;
if (x==0||y==0)
return 0;
if (x < 0)
x = -x;
if (y < 0)
y = -y;
if (x < y)
{
t = x;
x = y;
y = t;
}
while((t = x%y)!=0)
{
x = y;
y = t;
}
return y;
}
inline long lcm(long x,long y)//最小共倍数
{ // get the least common multiple of two integer(LCM)
return (x / gcd(x,y) * y);
}
推荐
猜你喜欢
© 2024 79432.Com All Rights Reserved.
电脑版|手机版