JAVA_求最小公倍数

耶也夜

关注

阅读 59

2022-04-29

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)

0 0 举报