在 Java 中实现欧几里得算法

Implementing Euclid's Algorithm in Java

我一直在尝试在 Java 中为 2 个数字实现欧几里得算法,或者 more.The 我的代码的问题是

a) 它适用于 2 个数字,但是当超过 2 个数字时 returns 多次正确值 entered.My 猜测这可能是因为 return 我的代码中的语句。

b) 我不太明白它是怎么回事 works.Though 我自己编码的,我不太明白 return 语句是如何工作的。

import java.util.*;

public class GCDCalc {

static int big, small, remainder, gcd;

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    // Remove duplicates from the arraylist containing the user input.

    ArrayList<Integer> listofnum = new ArrayList();
    System.out.println("GCD Calculator");
    System.out.println("Enter the number of values you want to calculate the GCD of: ");
    int counter = sc.nextInt();

    for (int i = 0; i < counter; i++) {
        System.out.println("Enter #" + (i + 1) + ": ");
        int val = sc.nextInt();
        listofnum.add(val);
    }

    // Sorting algorithm.
    // This removed the need of conditional statements(we don't have to
    // check if the 1st number is greater than the 2nd element
    // before applying Euclid's algorithm.
    // The outer loop ensures that the maximum number of swaps are occurred.
    // It ensures the implementation of the swapping process as many times
    // as there are numbers in the array.
    for (int i = 0; i < listofnum.size(); i++) {
        // The inner loop performs the swapping.
        for (int j = 1; j < listofnum.size(); j++) {
            if (listofnum.get(j - 1) > listofnum.get(j)) {
                int dummyvar = listofnum.get(j);
                int dummyvar2 = listofnum.get(j - 1);
                listofnum.set(j - 1, dummyvar);
                listofnum.set(j, dummyvar2);

            }
        }
    }

    // nodup contains the array containing the userinput,without any
    // duplicates.
    ArrayList<Integer> nodup = new ArrayList();
    // Remove duplicates.
    for (int i = 0; i < listofnum.size(); i++) {
        if (!nodup.contains(listofnum.get(i))) {
            nodup.add(listofnum.get(i));
        }
    }

    // Since the array is sorted in ascending order,we can easily determine
    // which of the indexes has the bigger and smaller values.
    small = nodup.get(0);
    big = nodup.get(1);
    remainder = big % small;

    if (nodup.size() == 2) {
        recursion(big, small, remainder);
    } else if (nodup.size() > 2) {
        largerlist(nodup, big, small, 2);
    } else // In the case,the array only consists of one value.
    {
        System.out.println("GCD: " + nodup.get(0));
    }
}

// recursive method.
public static int recursion(int big, int small, int remainder) {
    remainder = big % small;
    if (remainder == 0) {
        System.out.println(small);
    } else {
        int dummyvar = remainder;
        big = small;
        small = dummyvar;
        recursion(big, small, remainder);
    }
    return small;
}

// Method to deal with more than 2 numbers.
public static void largerlist(ArrayList<Integer> list, int big, int small, int counter) {
    remainder = big % small;
    gcd = recursion(big, small, remainder);

    if (counter == list.size()) {

    } else if (counter != list.size()) {
        big = gcd;
        small = list.get(counter);
        counter++;
        largerlist(list, gcd, small, counter);
    }

  }
}

对于任何格式错误等,我提前表示歉意。 任何建议都是 appreciated.Thanks!

我认为这两个作业是错误的方式

    big = gcd;
    small = list.get(counter);

然后big没用

    largerlist(list, gcd, small, counter);

您还使用了静态变量,这通常是个问题。

我建议删除 static/global 个变量并且通常不要重复使用变量。

编辑:哦,是的,return。从 recursion 方法调用时,您忽略了 recursion 方法的 return 值。这应该无关紧要,因为您正在打印而不是 returning 值,但是当您想要多次使用该函数时,此类解决方案会中断。