问题:给定一个由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
我不知道如何解决它。 请帮助我更正我的代码...谢谢!!!
答案 0 :(得分:0)
Lcm()函数中未定义gcd。最后定义的gcd在Gcd()函数中,尽管这不是全局变量Lcm()无法使用它。
也许你是这个意思?
static int Lcm(int[] newArray){
int multi =newArray[0];
int gcd = 0;
for(int i=1; i< newArray.length; i++){
gcd = getGcd(multi, newArray[i]);
if(gcd == 1) {
multi = multi * newArray[i];
}else {
multi = multi * newArray[i] / gcd;
}
}
return multi;
}
使用它可以生成结果。 您可以对此有所考虑。 我们从2开始,multi为2,下一个值为7。2和7的gcd为1,所以lcd为14。然后我们用14比较3,gcd也为1,所以将它们相乘就是42.然后使用42比较9,我们发现9具有42的gcd是3,所以我们使用42/3是14,然后使用14和9,gcd是1,给出14x9是126。然后使用126和4 ,则gcd为2,因此multi为126/2 = 63,而63和4 gcd为1,所以最终multi为63x4为252。
如果您看到任意两个数字,则无论是[3,7],[50,5],[20,10],[100,10000],[52,57],[3,81] ],则lcm始终为a * b / gcd。在这种情况下,我们可以获得答案。
答案 1 :(得分:0)
我希望“本逻辑”对您有所帮助。
// Java Program to find LCM of n elements
public class GFG {
public static long lcm_of_array_elements(int[] element_array)
{
long lcm_of_array_elements = 1;
int divisor = 2;
while (true) {
int counter = 0;
boolean divisible = false;
for (int i = 0; i < element_array.length; i++) {
// lcm_of_array_elements (n1, n2, ... 0) = 0.
// For negative number we convert into
// positive and calculate lcm_of_array_elements.
if (element_array[i] == 0) {
return 0;
}
else if (element_array[i] < 0) {
element_array[i] = element_array[i] * (-1);
}
if (element_array[i] == 1) {
counter++;
}
// Divide element_array by devisor if complete
// division i.e. without remainder then replace
// number with quotient; used for find next factor
if (element_array[i] % divisor == 0) {
divisible = true;
element_array[i] = element_array[i] / divisor;
}
}
// If divisor able to completely divide any number
// from array multiply with lcm_of_array_elements
// and store into lcm_of_array_elements and continue
// to same divisor for next factor finding.
// else increment divisor
if (divisible) {
lcm_of_array_elements = lcm_of_array_elements * divisor;
}
else {
divisor++;
}
// Check if all element_array is 1 indicate
// we found all factors and terminate while loop.
if (counter == element_array.length) {
return lcm_of_array_elements;
}
}
}
// Driver Code
public static void main(String[] args)
{
int[] element_array = { 2, 7, 3, 9, 4 };
System.out.println(lcm_of_array_elements(element_array));
}
}
答案 2 :(得分:0)
对于两个数字的GCD逻辑为,
// Java program to find GCD of two numbers
class Test
{
// Recursive function to return gcd of a and b
static int gcd(int a, int b)
{
// Everything divides 0
if (a == 0 || b == 0)
return 0;
// base case
if (a == b)
return a;
// a is greater
//u r missing the logic here.....................
if (a > b)
return gcd(a-b, b);
else
return gcd(a, b-a);
}
// Driver method
public static void main(String[] args)
{
int a = 98, b = 56;
System.out.println("GCD of " + a +" and " + b + " is " + gcd(a, b));
}
}
请查看逻辑,您应该迭代直到a = b,仅重复一次。