又稱歐幾里得演算法,
非遞回實作:
int gcd(int a,int b){
int c=a%b;
while(c!=0){
a=b;
b=c;
c=a%b;
}
return b;
}
遞回實作:
int gcd(int a,int b){
if(b==0){
return a;
}else{
return gcd(b,a%b);
}
}
利用最大公約數求最小公倍數:
int lcm(int a,int b){
return a*b/gcd(a,b);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/49377.html
標籤:C
上一篇:多行字串寫法
