java实现求最小公倍数_求最小公倍数(java)
import java.util.*;public class GetLCM {public static void main(String[] args) {Scanner scan = new Scanner(System.in);int a = scan.nextInt();int b = scan.nextInt();int mod = a % b;if(mod == 0)System.o
·
import java.util.*;
public class GetLCM {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
int b = scan.nextInt();
int mod = a % b;
if(mod == 0)
System.out.println(a);
//
int out = getMaxMult(a,b);
int result = a*b/out;
System.out.println(result);
}
private static int getMaxMult(int a, int b) {
int m = a % b;
while (m != 0) {
a = b;
b = m;
m = a % b;
}
return b;
}
}
注意:
1、先求出最大公约数,利用两数的乘积除以最大公约数得到最小公倍数
2、考虑两数整除的情况
更多推荐
已为社区贡献3条内容
所有评论(0)