题解 | #求最小公倍数#
求最小公倍数
https://www.nowcoder.com/practice/22948c2cad484e0291350abad86136c3
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] strings = in.nextLine().split(" "); Node node = new Node(Integer.parseInt(strings[0]), Integer.parseInt(strings[1])); int result = 1; while (!isDoublePrime(node)) { result *= node.num; node.x1 /= node.num; node.x2 /= node.num; } System.out.println(result * node.x1 * node.x2); } public static Boolean isDoublePrime(Node node) { for (int i = Math.min(node.x1, node.x2); i > 1; i--) if (node.x1 % i == 0 && node.x2 % i == 0) { node.num = i; return false; } return true; } } class Node { int x1; int x2; int num = 1; // 公因数 public Node(int x1, int x2) { this.x1 = x1; this.x2 = x2; } }