求两个数的最大公约数和最小公倍数用c++
来源:学生作业帮 编辑:作业帮 分类:数学作业 时间:2024/05/14 19:50:00
求两个数的最大公约数和最小公倍数用c++
是辗转法
代码:
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);
}
代码:
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);
}
c语言题:求两个数的最小公倍数和最大公约数
C语言:求两个数的最小公倍数和最大公约数
用Java求两个数的最大公约数和最小公倍数
求两个数的最大公约数和最小公倍数用c++
用C语言怎样求两个数的最大公约数和最小公倍数
c语言:求两个数的最大公约数与最小公倍数
c语言,输入两个正整数m和n,求两个数的最大公约数与最小公倍数
求两个m.n的最大公约数和最小公倍数 m*n\最大公约数=最小公倍数 c语言编程
C语言编程中,求两个数的最大公约数和最小公倍数算法是怎样的
求两个数的最大公约数和最小公倍数的公式是什么?
求两个数的最大公约数和最小公倍数的算法
c语言编程,编写程序,求两个数的最大公约数和最小公倍数