HDU 2028 Lowest Common Multiple Plus

Problem Description
求n個(gè)數(shù)的最小公倍數(shù)。

Input
輸入包含多個(gè)測試實(shí)例,每個(gè)測試實(shí)例的開始是一個(gè)正整數(shù)n,然后是n個(gè)正整數(shù)。

Output
為每組測試數(shù)據(jù)輸出它們的最小公倍數(shù),每個(gè)測試實(shí)例的輸出占一行。你可以假設(shè)最后的輸出是一個(gè)32位的整數(shù)。

Sample Input

2 4 6 3 2 5 7

Sample Output

12 70

Author
lcy

java code(兩兩求最小公倍數(shù)、輾轉(zhuǎn)相除法)

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;
    }

}```
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容