我的Euclid算法工作得很慢

时间:2013-09-22 22:00:58

标签: java algorithm

这段代码编译得很好,但是当我运行它时,它会按预期要求我的两个数字然后只是坐在那里并且根本不做任何事情。我已经搜索了互联网,整天都在努力。我终于崩溃了并寻求帮助。

问题是它没有自动循环回来吗?经过10个小时,我什么也没找到。

import java.util.Scanner;

public class EA
{
    public static void main (String[] args)
    {
        // get first integer from user
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter the larger integer: ");
        int I;
        I = input.nextInt();

        // get second integer from user
        System.out.println("Please enter the smaller integer: ");
        int J;
        J = input.nextInt();

        //resolve the issue of zero
        while(J<1)
        {
            System.out.println("Can not divide by zero!");
            System.out.println("Please enter new smaller integer: ");
            J = input.nextInt();

            //do the calculations
            while(J>0)
            {
                int Remainder;
                Remainder = I % J;

                while(Remainder>0)
                {
                    I = J;
                    J = Remainder;

                    return;

                }
                System.out.println("GCD is" + J);
            }
        }
    }
}

5 个答案:

答案 0 :(得分:2)

SJuan提到返回打破循环,这是真的,但即使它已修复,还有一些其他问题:

  • 内心永远不会结束(无限循环)
  • 结果将存储在J - 而不是I
  • System.out.println("GCD is " + I);应该在外面打印!

你的程序的“核心”应该这样做:

    // we get here with valid values stored in I,J
    int Remainder  = I % J;
    //do the calculations
    while(Remainder>0)
    {
        I = J;
        J = Remainder;
        Remainder  = I % J;
    }
    System.out.println("GCD is " + J);

答案 1 :(得分:2)

在已经提及的其他事项中,您将whileif混为一谈。您已将算法逻辑放在while循环中,该循环仅在第一个输入错误时运行。

// get first integer from user
Scanner input = new Scanner(System.in);
System.out.println("Please enter the larger integer: ");
int I;
I = input.nextInt();

// get second integer from user
System.out.println("Please enter the smaller integer: ");
int J;
J = input.nextInt();

//resolve the issue of zero
while(J<1)
{
    // You never reach here under ordinary conditions
}

答案 2 :(得分:0)

循环中间的return将结束执行。

这一个

 while(Remainder>0)
 {
     I = J;
     J = Remainder;

    return; <------- THIS IS THE RETURN THAT BREAKS ALL

 }

所以它没有到达System.out.println

更新:此外,您input.nextInt()两次J。可能从你的描述中,它一直在等你输入第三个整数。

答案 3 :(得分:0)

有多个错误:while中的返回,算法和第一次的括号。

1)解决零问题后,必须在重新赋值变量J后突然关闭while的括号。

while (J < 1) {
    System.out.println("Can not divide by zero!");
    System.out.println("Please enter new smaller integer: ");
    J = input.nextInt();
}

2)计算gcd的算法如下:

function gcd(a, b)
    while b ≠ 0
       t := b
       b := a mod t
       a := t
    return a

以下是您的代码的正确版本:

public static void main(final String[] args) {
    // get first integer from user
    final Scanner input = new Scanner(System.in);
    System.out.println("Please enter the larger integer: ");
    int I;
    I = input.nextInt();

    // get second integer from user
    System.out.println("Please enter the smaller integer: ");
    int J;
    J = input.nextInt();

    // resolve the issue of zero
    while (J < 1) {
        System.out.println("Can not divide by zero!");
        System.out.println("Please enter new smaller integer: ");
        J = input.nextInt();
    }
    // do the calculations
    while (J != 0) {
        int Remainder;
        Remainder = I % J;
        I = J;
        J = Remainder;
    }
    System.out.println("GCD is" + I);

}

答案 4 :(得分:0)

Euclid算法有一个缺点,因为两个输入都应该非零以计算最大公约数。但是如果你想在其中一个输入是零(&#39; 0&#39;)时找出GCD,那么可以稍微调整一下逻辑。当其中一个输入为零时,GCD为1,并且&#39; a&#39;应该大于&#39; b&#39;计算GCD。请查看以下代码段:

    if (a < b) {
        int temp = a;
        a = b;
        b = temp;
    }
    if (b == 0) {
        System.out.println("1");
    } else {
        while (b != 0) {
            r = a % b;
            a = b;
            b = r;
        }