简体   繁体   English

为什么我的程序在某些情况下输出不正确?

[英]Why is my program giving an incorrect output in certain cases?

I have made an implementation of the Euclidean Algorithm in Java to find the Greatest Common Divisor (GCD) of two given numbers. 我已经在Java中实现了欧几里德算法,以找到两个给定数字的最大公约数(GCD)。

For the most part, my program works fine, I have tested it with a few random sets of numbers, although, I have found in one case (that I know of) that it's giving an incorrect output, which is for the following combination of numbers: 在大多数情况下,我的程序运行正常,我已经用一些随机的数字集进行了测试,但是,我发现在一个案例中(我知道)它给出了一个不正确的输出,这是针对以下组合的数字:

Enter integer a: 8965 Enter integer b: 55 输入整数a:8965输入整数b:55

The output of the program should be 55, although this is not the case. 该计划的输出应为55,但情况并非如此。 The out given is as follows: 给出的结果如下:

gcd = 1 Execution time: 0.005747ms. gcd = 1执行时间:0.005747ms。

I'm not sure why this particular combination of numbers is causing a problem, as it works fine for other numbers, for example, here is the results for a different set of numbers: 我不确定为什么这个特定的数字组合会导致问题,因为它适用于其他数字,例如,这是一组不同数字的结果:

Enter integer a: 15000 输入整数a:15000

Enter integer b: 5325 输入整数b:5325

gcd = 75 gcd = 75

Execution time: 0.007389ms. 执行时间:0.007389ms。

import java.util.Scanner;
public class EuclideanAlgorithm {
    public static void main (String [] args) {
        int a, b;
        try(Scanner sc = new Scanner(System.in);) {
            System.out.print("Enter integer a: ");
            a = sc.nextInt();
            System.out.print("Enter integer b: ");
            b = sc.nextInt();
        }
        long start = System.nanoTime();
        int answer = EuclideanAlgorithm(a, b);
        long stop = System.nanoTime();
        System.out.println("gcd = " + answer);
        System.out.println("Execution time: " + ((stop - start) / 1e+6) + "ms.");

    }

    public EuclideanAlgorithm() {}; //Suppress default constructor

    private static int EuclideanAlgorithm(int a, int b) {
        if ( (a == 0) || (b == 0)) {
            return 0;
        }
        if (b > a) {
            int temp = a;
            a = b;
            b = temp;
        }
        int gcd = 1;
        while(gcd != 0) {
            if( (a % b) == 0) {
                break;
            }
            gcd = a % b;
            a  = b;
            b = gcd;
        }
        return gcd;
    }
}

Whenever one of your numbers a , b is a multiple of the other, then your if condition will cause a break and 1 will be returned, which is incorrect. 每当你的a数字ab是另一个数字a倍数时,那么你的if条件将导致break并且将返回1 ,这是不正确的。 But the rest of the algorithm is incorrect also. 但算法的其余部分也是不正确的。

According to the pseudocode for the Euclidean Algorithm : 根据欧几里得算法的伪代码:

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

You need to check if b is not 0 , not the gcd. 你需要检查b是不是0 ,而不是gcd。 You'll need to modify your code to match this algorithm; 您需要修改代码以匹配此算法; your code is not currently matching this algorithm. 您的代码目前不符合此算法。

Because of the if condition inside this while loop 因为while while循环中的if条件

int gcd = 1;
while(gcd != 0) {
    if( (a % b) == 0) {
        break;
    }
    gcd = a % b;
    a  = b;
    b = gcd;
}

So, in case a % b = 0 at the beginning -> result is always equaled to 1. 因此,如果开头的%b = 0 - >结果总是等于1。

You need to handle that case separately. 您需要单独处理该案例。

int gcd = b;
while(a % b != 0){
   gcd = a % b;
   a = b;
   b = gcd;
}

It's easy 55 divides 8965 that means you programm breaks in the first line and returns your initial value which is 1. 很容易55分8965这意味着你在第一行打破程序并返回你的初始值1。

Instead something like this could help. 相反,像这样的东西可以帮助。

int gcd = 1;
if( (a % b) == 0) {
   return b;
}
while(gcd != 0) {
    if( (a % b) == 0) {
        break;
    }
    gcd = a % b;
    a  = b;
    b = gcd;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM