求最大公約數和最小公倍數

3年以前  |  閱讀數:159 次  |    
package homework;

import java.util.Scanner;
public class 最大公約數和最大公倍數 {
    public static int GCD(int a,int b){
        int tmp=1;
        for(int i=0;;i++){
            tmp=a%b;
            if(tmp==0){
                break;
            }
            a=b;
            b=tmp;
        }
        return b;
    }
    public static int LCM(int a,int b){
        int temp=GCD(a,b);
        return (a*b)/temp;
    }
    public static void main(String[] args) {
        int m,n;
        Scanner reader=new Scanner(System.in);
        System.out.println("enter m and n");
        m=reader.nextInt();
        n=reader.nextInt();
        int gcd,lcm;  //gcd 最大公約數,LCM最小公倍數
        gcd=GCD(m,n);
        lcm=LCM(m,n);
        System.out.print("最大公因數:");
        System.out.println(gcd);
        System.out.print("最小公倍數:");
        System.out.println(lcm);
    }
}

相關文章:

18禁止午夜福利体验区,人与动人物xxxx毛片人与狍,色男人窝网站聚色窝,女生把筷子放屁眼里,国产精品久久久,国产日产欧洲无码视频