2 4 6 3 2 5 7
12 70
分析:两个数的最小公倍数 lcm( x , y ) = x * y / gcd( x , y )。其中 gcd() 是这两个数的最大公约数,可以采用“辗转相除法”求解。所以这题的关键是求最大公约数。
import java.util.Scanner;
public class Main {
// 最大公约数
static long gcd(long a, long b) {
long remainder = a % b;
while (remainder != 0) {
a = b;
b = remainder;
remainder = a % b;
}
return b;
}
// 最小公倍数
static long lcm(long a, long b) {
return a * b / gcd(a, b);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int n = scanner.nextInt();
long temp = 1;
for (long i = 0; i < n; i++) {
long num = scanner.nextLong();
temp = lcm(temp, num);
}
System.out.println(temp);
}
}
}[hdu 2028] Lowest Common Multiple Plus,布布扣,bubuko.com
[hdu 2028] Lowest Common Multiple Plus
原文:http://blog.csdn.net/u011506951/article/details/23618581