/**
* @author:朱凌风
* @weather:多云转晴
* @date:06/18/2011
* @function:求两个数的最小公倍数和最大公约数
*/
package com.jerome;
public class MaxAndMin {
public static void main(String[] args) {
GreatestCommonDivisor gcd=new GreatestCommonDivisor(26,4);
System.out.println("最大公约数是:"+gcd.calculate());
LeastCommonMultiple lcm=new LeastCommonMultiple(26,4);
System.out.println("最小公倍数是是:"+lcm.calculate());
}
}
//最大公约数类
class GreatestCommonDivisor {
int m=0;
int n=0;
GreatestCommonDivisor(int m,int n) {
this.m=m;
this.n=n;
}
//计算最大公约数
public int calculate(int m,int n) {
int temp=0;
if(m<n) {
//交换m与n
temp=n;
n=m;
m=temp;
}
if(n==0) {
return m;
}else {
return calculate(n,m%n);
}
}
public int calculate() {
return this.calculate(this.m,this.n);
}
}
//最小公倍数类
class LeastCommonMultiple {
int m=0;
int n=0;
LeastCommonMultiple(int m,int n) {
this.m=m;
this.n=n;
}
//计算最小公倍数
public int calculate() {
GreatestCommonDivisor gcd=new GreatestCommonDivisor(this.m,this.n);
int min=this.m*this.n/gcd.calculate();
return min;
}
}