题解:辗转相除法求最大公约数,最小公倍数=ab/gcd(a,b);防止ab太大,可以a/gcd(a,b)*b;
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
//辗转相除法求最大公约数
int gcd(int x,int y)
{
int z;
while(y)
{
z=x%y;
x=y;
y=z;
}
return x;
}
//递归法求最大公约数
int gcdd(int x,int y)
{
return y==0?x:gcdd(y,x%y);
}
//求最小公倍数
int lcm(int x,int y)
{
//return x*y/gcd(x,y);//x*y可能会溢出
return x/gcd(x,y)*y;//此种方法会更优化
}
int main()
{
cout<<gcd(24,8)<<endl;
cout<<gcdd(24,8)<<endl;
cout<<lcm(24,8)<<endl;
return 0;
}