问题:给定一个包含 n 个数字的数组,找到它的 LCM。
由于 LCM(a,b) = a*b / GCD(a,b),这是我的原始代码:
class GFG {
// the simple 2 integers recursive to find their GCD.
static int getGcd(int a, int b){
while(a != b){
if(a >b){
a = a-b;
}else{
b= b-a;
}
}
return a;
}
// find GCD in an array that has more than two integers.
static int Gcd(int[] newArray){
int gcd = newArray[0];
for(int i=1; i<newArray.length; i++){
gcd = getGcd(gcd,newArray[i]);
}
return gcd;
}
static int Lcm(int[] newArray){
int multi =1;
for(int i=0; i< newArray.length; i++){
multi = multi* newArray[i];
}
return (multi/gcd);
}
public static void main (String[] args) {
int[] newArray = { 2, 7, 3, 9, 4 };
System.out.println(Gcd(newArray));
System.out.println(Lcm(newArray));
}
}
但是当我运行这段代码时,它显示了一些错误:
prog.java:33: error: cannot find symbol
return (multi/gcd);
^
symbol: variable gcd
我不知道如何解决它。请帮我更正我的代码......谢谢!