Problem Description
求n個(gè)數(shù)的最小公倍數(shù)。
Input
輸入包含多個(gè)測(cè)試實(shí)例,每個(gè)測(cè)試實(shí)例的開(kāi)始是一個(gè)正整數(shù)n,然后是n個(gè)正整數(shù)。
Output
為每組測(cè)試數(shù)據(jù)輸出它們的最小公倍數(shù),每個(gè)測(cè)試實(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;
}
}```