用Java实现Euclid算法

时间:2015-12-25 17:59:22

标签: java return greatest-common-divisor

我一直在尝试用Java实现Euclid的算法2个或更多。我的代码的问题是

a)它适用于2个数字,但是当输入2个以上的数字时,它会多次返回正确的值。我猜是这可能是因为我的代码中的return语句。

b)我不太明白它是如何工作的。尽管我自己编写了代码,但我并不完全理解返回语句是如何工作的。

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);
    }

  }
}

我提前为任何格式错误等道歉。 任何建议都将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

我认为这两项任务是错误的方式

update table1 
set table1.quantity = table1.quantity - SUM( table2.sales ) 
from table1
inner join table2 on table1.productID = table2.productID
where table1.productID = 1;

然后 big = gcd; small = list.get(counter); 未使用

big

你也使用过静态变量,这通常是一个问题。

我建议删除静态/全局变量,通常不重用变量。

编辑:哦,是的, largerlist(list, gcd, small, counter); 。从return方法调用时,您忽略了recursion方法的返回值。这应该无关紧要,因为您打印出来而不是返回值,但是当您想要多次使用该函数时,此类解决方案会中断。