Q:求兩個(gè)數(shù)的最大公約數(shù)和最小公倍數(shù)
A:輾轉(zhuǎn)相除法
//求兩個(gè)數(shù)的最大公約數(shù)
//a=k*b+c,那么a,b的最大公約數(shù)也就是b,c的最大公約數(shù)
public static int commonChild(int a,int b)
{
while (a%b!=0)
{
int c=a%b;
a=b;
b=c;
}
return b;
}
//a,b的最小公倍數(shù)等于a*b/(a,b的最大公約數(shù))
public static int commonParent(int a,int b)
{
return a*b/commonChild(a,b);
}