题解 | #求最小公倍数#
求最小公倍数
http://www.nowcoder.com/practice/feb002886427421cb1ad3690f03c4242
先求出两个数中的最大值,然后从最大数到两个数的乘积进行遍历,如果遇到一个数可以同时整除m和n,就返回
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) {
int a = m * n;
int max = Math.max(m,n);
for (int i = max; i <= a; i++) {
if (i % m == 0 && i % n == 0) {
return i;
}
}
return a;
}
}