题解 | #求最小公倍数#
求最小公倍数
http://www.nowcoder.com/practice/22948c2cad484e0291350abad86136c3
import java.util.*;
public class Main { public static LinkedList list = new LinkedList<>(); public static int reslut = 1;
public static void main(String[] agrs) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
if (a == b) {
System.out.print(a);
return;
}
if (a > b) {
if (a %b == 0){
System.out.println(a);
return;
}
getMax(a, b);
for (int i : list) {
reslut = i * reslut;
}
System.out.print(reslut);
return;
} else {
if (b %a == 0){
System.out.println(b);
return;
}
getMax(b, a);
for (int i : list) {
reslut = i * reslut;
}
System.out.print(reslut);
return;
}
}
public static void getMax(int a, int b) {
for (int i = 2; i <= b; i++) {
if (a % i == 0 && b % i == 0) {
list.add(i);
list.add(a / i);
list.add(b / i);
getMax(a / i, b / i);
}
if (i == b && list.size() == 0) {
list.add(a);
list.add(b);
}
}
}
}