package
com.charles.algo;
/**
* @author charles.wang
* 题目:输入两个正整数m和n (m>n),求其最大公约数和最小公倍数
*/
public
class
GongYueShuGongBeiShu {
private
GongYueShuGongBeiShu(){}
/**
* 用辗转相除法来计算最大公约数
*/
public
static
int
maxGongYue(
int
m,
int
n){
int
temp;
if
(m<n){
temp= n;
n=m;
m=temp;
}
int
remain = m % n;
if
(remain==
0
)
return
n;
return
maxGongYue( n,remain);
}
/**
* 最小公倍数的值为二个数的乘积除以最大公约数
*/
public
static
int
minGongBei(
int
m,
int
n){
return
m*n/maxGongYue(m,n);
}
/**
* @param args
*/
public
static
void
main(String[] args) {
int
m =
12
;
int
n =
18
;
System.out.println(
"输入数为:"
+m+
" ,"
+n);
System.out.println(
"最大公约数为:"
+maxGongYue(m,n));
System.out.println(
"最小公倍数为:"
+minGongBei(m,n));
}
}