题解 | HJ108#求最小公倍数#
求最小公倍数
https://www.nowcoder.com/practice/22948c2cad484e0291350abad86136c3
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
boolean flag = true;
int num = 1;
//a<b时b可能是最小公倍数 若不是则寻求一个B的倍数中也是A的倍数的最小数字
if (a < b) {
if (b % a == 0) {
System.out.println(b);
} else {
while (flag) {
int multiplyRes = Math.multiplyExact(b, num);
if (multiplyRes % a == 0) {
System.out.println(multiplyRes);
flag = false;
}
num++;
}
}
} else {
if (a % b == 0) {
System.out.println(a);
} else {
while (flag) {
int multiplyRes = Math.multiplyExact(a, num);
if (multiplyRes % b == 0) {
System.out.println(multiplyRes);
flag = false;
}
num++;
}
}
}
}
}
//a<b时,若b是a的倍数则b是a的最小公倍数,若不是则寻求一个B的倍数中也是A的倍数的最小数字。
#华为od#
海康威视公司福利 1262人发布

