天天看點

我遇到的最優的最大公約數與最小公倍數的算法

int zuixiaogongbeishu(int a,int b)//最小公倍數  

{

int c,d,t;

c = b;

d = a;

while (c) 

{

t = d%c;

d = c; 

c = t;

}

return (a*b)/d;

}

int zuidagongyueshu(int a,int b)//最大公約數是

{

int c,d,t;

c = b;

d = a;

while (c) 

{

t = d%c;

d = c; 

c = t;

}

return d;

}

繼續閱讀