问题描述
一组正整数的最小公倍数 (LCM) 是最小的正整数,可以被该集合中的所有数字整除。例如,5、7 和 15 的 LCM 为 105。
输入
输入将包含多个问题实例。输入的第一行将包含一个整数,指示问题实例的数量。每个实例将由一行组成,格式为 m n1 n2 n3 ...nm 其中 m 是集合中的整数数,n1 ...nm 是整数。所有整数都是正数,并且位于 32 位整数的范围内。
输出
对于每个问题实例,输出包含相应 LCM 的单行。所有结果都将位于 32 位整数的范围内。
示例输入
2 3 5 7 15 6 4 10296 936 1287 792 1
示例输出
105 10296
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextLong();
}
long x = arr[0];//最小公倍数
aa:for (int i = 1; i < n; i++) {
if(x%arr[i]==0) continue aa;
else if(arr[i]%x==0) x=arr[i];
else {
long a = 0;//最大公约数
for (long j = Math.min(arr[i], x); j > 1; j--) {
if (arr[i] % j == 0 && x % j == 0) {
a = j;
break;
}
}
if(a==0) x=arr[i]*x;
else x = (long)(arr[i] * x )/ a;
}
}
System.out.println(x);
}
}
}