这是用于评估给定整数数组的因子的代码。问题是我需要从我收到的输出列表中找到 LCM。建议从列表中删除重复元素
主要方法
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int [] ar = {6,12,36};
for (int a:
ar) {
System.out.println(getFactor(a));
}
getFactor(long) 返回因子列表
private static List<Integer> getFactor(long n) {
List<Integer> l = new ArrayList<>();
for (int i=2;n!=1;i++)
{
if(n%i==0)
{
l.add(i);
n=n/i;
i=1;
}
}
return l;
}
/*Input
6,12,36
Output
[2, 3]
[2, 2, 3]
[2, 2, 3, 3]*/
问题 从其他列表中删除 [2,3] 重复项以获得 LCM(最小公倍数)。
最佳答案
代码中没有计算 lcm 的位置。 我只看到你已经找到了三个数字的因数。
[2, 3] -> Factors of 6
[2, 2, 3] -> Factors of 12
[2, 2, 3, 3] -> -> Factors of 6
您必须编写另一个函数来计算 lcm。
关于java - 从下面的程序中的不同列表中删除公共(public)元素的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60350730/