0
点赞
收藏
分享

微信扫一扫

JAVA_求最小公倍数

耶也夜 2022-04-29 阅读 58

JAVA_求最小公倍数

题目链接

在这里插入图片描述

思路:

代码:

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){
        //write your code here......
         //从max到m*n之间找最小公倍数
        for( int i= (m>n?m:n);i<=m*n;i++){
             //如果既能被m整除又能被n整除,说明是最小公倍数,直接返回
            if(i%n==0&&i%m==0){
                return i;
            }
        }
        return -1;
    }
}
举报

相关推荐

0 条评论