求两个数的最大公约数和最小公倍数用c++

学习 时间:2026-03-30 15:41:01 阅读:4993
求两个数的最大公约数和最小公倍数用c++

最佳回答

土豪的母鸡

无私的发带

2026-03-30 15:41:01

是辗转法 代码: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); }

最新回答共有2条回答

  • 可靠的期待
    回复
    2026-03-30 15:41:01

    是辗转法 代码: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); }

上一篇 用初等行变化求矩阵的逆矩阵时,用定义AB=BA=E求,计算量太大!有什么简便的方法 求矩阵的

下一篇 重新排列下列句子都要听别人说话,看别人写的东西。学语文就是学习听、说、读、写的本领。就是把自己要告诉别人的事情和意思说出