您的位置:首页 > 其它

最小公倍数----最大公约数

2018-03-26 21:29 225 查看
package com.xjj.algorithm;

import java.util.Scanner;

public class Gcd {

//欧几里得算法求最大公约数
public int gcd(int m, int n){
if (n == 0)
return m;
return gcd(n, m % n);
}

//最小公倍数
public int lcm(int m, int n){
return m * n /gcd(m, n);
}

public static void main(String[] args) {
System.out.println("输入两个数: ");
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
Gcd gcd = new Gcd();
System.out.println("最大公约数为: " + gcd.gcd(m, n));
System.out.println("最小公倍数为: " + gcd.lcm(m, n));

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: