题解 | #求最小公倍数#
求最小公倍数
http://www.nowcoder.com/practice/feb002886427421cb1ad3690f03c4242
import java.util.*;
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 gcd(int a, int b) {
//取差的绝对值
int temp = (a - b) > 0 ? a - b : -(a - b);
//不断减去差的绝对值直到为0
while (temp != 0) {
a = b;
b = temp;
temp = (a - b) > 0 ? a - b : -(a - b);
}
return b;
}
public static int getCM(int m, int n) {
return m * n / (gcd(m, n)); //两数乘积除以最大公约数
}
}