1. 题目
108.求最小公倍数
2. 描述
正整数A和正整数B 的最小公倍数是指 能被A和B整除的最小的正整数值,设计一个算法,求输入A和B的最小公倍数。
输入描述:
输入两个正整数A和B。
输出描述:
输出A和B的最小公倍数。
示例1
输入
5 7
输出
35
3. 实现方法
3.1 方法 1
3.1.1 思路
最小公倍数即能同时被数字m和数字n整除的最小整数,利用欧几里得公式进行求解,先算出最大公约数,然后求出最小公倍数;
3.1.2 实现
import java.util.Scanner; /** * @author : cunyu * @version : 1.0 * @className : OneZeroEight * @date : 2020/8/8 22:41 * @description : 108.求最小公倍数 */ public class OneZeroEight { public static void main(String[] args) { Scanner input = new Scanner(System.in); while (input.hasNext()) { int m = Integer.parseInt(input.nextLine().split(" ")[0]); int n = Integer.parseInt(input.nextLine().split(" ")[1]); System.out.println(getLcm(m, n)); } } /** * @param m * @param n * @return * @description 求最大公约数 * @date 2020/8/8 22:50 * @author cunyu1943 * @version 1.0 */ public static int getGcd(int m, int n) { while (n > 0) { int tmp = m % n; m = n; n = tmp; } return m; } /** * @param m * @param n * @return * @description 求最小公倍数 * @date 2020/8/8 22:50 * @author cunyu1943 * @version 1.0 */ public static int getLcm(int m, int n) { return m * n / getGcd(m, n); } }