题解 | #求最小公倍数#

求最小公倍数

http://www.nowcoder.com/practice/22948c2cad484e0291350abad86136c3



import java.util.Scanner;

/**
 * @ProjectName: leetcode
 * @Package: com.company.niuke
 * @ClassName: Lcm
 * @Author: Administrator
 * @Description: 最小公倍数
 * @Date: 2022/2/11 22:38
 * @Version: 1.0
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] num = scanner.nextLine().trim().split(" ");
        int i = Integer.valueOf(num[0]);
        int j = Integer.valueOf(num[1]);
        //求i和j的最小公倍数
        System.out.println(getLcm(i, j));
    }

    //使用欧几里得算法求解数m和数n最大公约数
    public static int getGcd(int m,int n){
        while(n > 0){
            int temp = m % n;
            m = n;
            n = temp;
        }
        return m;
    }

    //求解数m和n和最小公倍数
    public static int getLcm(int m,int n){
        int gcd = getGcd(m,n);
        int result = m*n / gcd;
        return result;
    }

}

全部评论

相关推荐

12-20 11:21
复旦大学 Java
点赞 评论 收藏
分享
12-13 14:51
已编辑
井冈山大学 算法工程师
龙虾x:算法比你强的没有你美,比你美的…..算了已经没有比你美的了
工作两年想退休了
点赞 评论 收藏
分享
评论
9
4
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务