-5

问题:给定一个包含 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

我不知道如何解决它。请帮我更正我的代码......谢谢!

4

3 回答 3

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,给 multi 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。在这种情况下,我们可以得到答案。

于 2018-07-11T03:11:30.923 回答
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 ,你只迭代一次。

于 2018-07-11T03:27:26.453 回答
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));
}
}
于 2018-07-11T03:23:08.200 回答