ericling

最大公约数GCD(Greatest Common Divisor)

最常见的求两个数的最大公约数的算法是辗转相除法,也叫欧几里得算法

该算法的c++语言实现如下:

#include<iostream>
using namespace std;
int gcd(int a,int b){
    return b==0?a:gcd(b,a%b);
}
int main(){
    int a=45,b=10;
    cout<<gcd(10,45);
}

Output

5

最小公倍数LCM(Lowest Common Multiple)

最大公倍数=a*b/最大公约数;

它的c++语言实现如下:

#include<iostream>
using namespace std;
int gcd(int a,int b){
    return b==0?a:gcd(b,a%b);
}
int lcm(int a,int b){
 	return a*b/gcd(a,b);
}
int main(){
    int a=45,b=10;
    cout<<gcd(10,45)<<endl;
    cout<<lcm(10,45);
    return 0;
}

Output

5
90

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-04-05
  • 2021-12-19
  • 2022-12-23
  • 2022-12-23
  • 2021-05-28
猜你喜欢
  • 2022-12-23
  • 2021-08-16
  • 2022-12-23
  • 2021-12-19
  • 2021-12-04
  • 2021-12-19
相关资源
相似解决方案