题解 | #求最小公倍数#

求最小公倍数

https://www.nowcoder.com/practice/feb002886427421cb1ad3690f03c4242

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        int m = console.nextInt();
        int n = console.nextInt();
        int result = getCM(m, n);
        System.out.println(result);
    }

    public static int getCM(int m, int n) {
        int x = 0;
        int M = m;
        int N = n;
        //write your code here......
        if (m < n)
            m = m + n - (n = m);
        //辗转相除法求最大公约数
        do {
            x = m % n;
            m = n;
            n = x;
        } while (x != 0);

        return (M / m) * N;//最小公倍数


    }
}

#java#
全部评论

相关推荐

点赞 评论 收藏
转发
1 收藏 评论
分享
牛客网
牛客企业服务