最小公倍数,a,b,最小公倍数为a*b/gcd(a,b)
1 #include<iostream> 2 3 using namespace std; 4 5 int gcd(int a,int b) 6 { 7 if(a<b) 8 { 9 int temp=b; 10 b=a; 11 a=temp; 12 } 13 int c; 14 while(b) 15 { 16 c=a%b; 17 a=b; 18 b=c; 19 } 20 return a; 21 } 22 int main() 23 { 24 int a,b,c; 25 while(cin>>a>>b>>c) 26 { 27 int temp=a*b/gcd(a,b); 28 int ans=temp*c/gcd(temp,c); 29 cout<<ans<<endl; 30 } 31 return 0; 32 }
本文介绍了一种求解三个整数最小公倍数的算法,通过先计算两数最大公约数,再利用该结果求解最小公倍数,最后扩展到三数情况。代码使用C++实现,展示了如何通过辗转相除法计算最大公约数。

被折叠的 条评论
为什么被折叠?



