HDU 2028 Lowest Common Multiple Plus

Problem Description
求n个数的最小公倍数。

Input
输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数。

Output
为每组测试数据输出它们的最小公倍数,每个测试实例的输出占一行。你可以假设最后的输出是一个32位的整数。

Sample Input

2 4 6 3 2 5 7

Sample Output

12 70

Author
lcy

java code(两两求最小公倍数、辗转相除法)

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        while (cin.hasNextInt()) {
            int n = cin.nextInt();
            long[] a = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = cin.nextInt();
            for (int i = 1; i < n; i++) {
                a[0] = a[0] * a[i] / getGongYue(a[0], a[i]);
            }
            System.out.println(a[0]);
        }
        cin.close();
    }
    public static long getGongYue(long a, long a2) {
        while (a % a2 != 0) {
            long temp = a % a2;
            a = a2;
            a2 = temp;
        }
        return a2;
    }

}```

你可能感兴趣的:(HDU 2028 Lowest Common Multiple Plus)