天天看点

java笔试之求最小公倍数

正整数A和正整数B 的最小公倍数是指 能被A和B整除的最小的正整数值,设计一个算法,求输入A和B的最小公倍数。

package test;

import java.util.Scanner;

public class exam06 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            System.out.println(min(a, b));
        }
        scanner.close();
    }

    // 求最大公约数
    public static int gcd(int a, int b) {
        return (b == 0) ? a : gcd(b, a % b);
    }

    public static int min(int a, int b) {
        return a * b / gcd(a, b);
    }
}      

转载于:https://www.cnblogs.com/bella-young/p/6409023.html