求两个数a,b的最大公约数m和最小公倍数n
分析:最大公约数m可以通过递归实现,最小公倍数n=a*b/m。
package com.pratice.daily;
public class MaxdivisorMinmultiple {
int minMultiple;
int maxDivisor;
int a;
int b;
public MaxdivisorMinmultiple(int a,int b){
this.a=a;
this.b=b;
}
public void multiple(){
if(maxDivisor==0){
System.out.println("application error!");
return;
}
else minMultiple=a*b/maxDivisor;
}
public void divisor(){
int x=a;
int y=b;
maxDivisor=findDivisor(x,y);
}
public int findDivisor(int x,int y){
if(y==0) return x;
else return findDivisor(y, x%y );
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MaxdivisorMinmultiple dm = new MaxdivisorMinmultiple(62,2);
dm.divisor();
dm.multiple();
System.out.println("maxDivisor: "+dm.maxDivisor+"");
System.out.println("minMultiple: "+dm.minMultiple+"");
}
}