求最小公倍數

題目描述

正整數A和正整數B 的最小公倍數是指 能被A和B整除的最小的正整數值,設計一個算法,求輸入A和B的最小公倍數。

輸入描述

輸入兩個正整數A和B。

輸出描述

輸出A和B的最小公倍數。

輸入例子

5
7

輸出例子

35

算法實現

import java.util.Scanner;

/**
 * All Rights Reserved !!!
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
//        Scanner scanner = new Scanner(Main.class.getClassLoader().getResourceAsStream("data.txt"));
        while (scanner.hasNext()) {
            int m = scanner.nextInt();
            int n = scanner.nextInt();

            System.out.println(m / gcd(m, n) * n);
        }

        scanner.close();
    }

    private static int gcd(int max, int min) {
        int tmp;
        if (max < min) {
            tmp = max;
            max = min;
            min = tmp;
        }

        while (max % min != 0) {
            tmp = min;
            min = max % min;
            max = tmp;
        }

        return min;
    }
}
相關文章
相關標籤/搜索