简体   繁体   English

使用递归并实现Euclid算法从用户中查找三个数字的GCD

[英]Using recursion and implementing Euclid's algorithm to find GCD of three numbers from user

I am wanting to ask the user to input three numbers and then have program calculate the GCD using Euclid's algorithm all the while using recursion. 我想请用户输入三个数字,然后让程序在使用递归的同时始终使用Euclid算法计算GCD。

My code right now implements two input numbers. 我的代码现在实现了两个输入数字。 I understand the approach of calculating the GCD of a and b, and calling it result d. 我了解计算a和b的GCD并将其称为结果d的方法。 Then using the third input (c) and d to find the GCD and essentially repeating Euclid's algorithm again; 然后使用第三个输入(c)和d查找GCD,并本质上再次重复Euclid算法; I am not sure how to implement this in code. 我不确定如何在代码中实现这一点。

import java.util.Scanner;

public class RecursionDemo {

public static void main (String[] args) {

Scanner userInput = new Scanner(System.in);

     System.out.println("Enter first number: ");
     int a = userInput.nextInt();

     System.out.println("Enter second number: ");
     int b = userInput.nextInt();


     System.out.println("GCD is: " + gCd(a, b));
   }

     public static int gCd(int a, int b) {

     if(b == 0){
         return a;
        }
     return gCd(b, a%b);         
   }
}   

The part that is really throwing me off is using recursion to solve my problem. 真正让我失望的部分是使用递归来解决我的问题。

So far I know I need to implement: 到目前为止,我知道我需要实现:

System.out.println("Enter third number: ");
     int c = userInput.nextInt();

d = //Not sure here

//And then modify my recursion method to find GCD.

Any help or suggestions would greatly be appreciated! 任何帮助或建议,将不胜感激!

d = gCd (a, b);
System.out.println("GCD is: " + gCd(d, c));

Note that you may call your gCd function with any two arguments, not just a and b . 请注意,您可以使用任意两个参数调用gCd函数,而不仅仅是ab For better understanding and less confusion, you may want to rename its arguments, like the following: 为了更好地理解和减少混乱,您可能希望重命名其参数,如下所示:

 public static int gCd(int x, int y) {
     if(y == 0) {
         return x;
     }
     return gCd(y, x%y);
 }

So, first you call it with x = a and y = b to find GCD of a and b . 因此,首先用x = ay = b调用它以找到ab GCD。 Store the result into new variable d . 将结果存储到新变量d After that, you call it again with x = d which is in turn GCD of a and b , and y = c . 之后,再次用x = d调用它, x = d又是ab GCD,并且y = c Thus you get the GCD of all the three numbers. 这样就得到了所有三个数字的GCD。

The gcd method can be iterated to obtain the gcd of a larger set of numbers. 可以迭代gcd方法以获得更大数字集的gcd。 For example: 例如:

gCd(a, b, c) = gCd( gCd(a, b), c)

and

gCd(a, b, c, d) = gCd( gCd(a, b, c), d) so then gCd(a, b, c, d) = gCd( gCd(a, b, c), d)因此
gCd(a, b, c, d) = gCd( gCd( gCd(a, b), c), d)

Easy, specific solution: 简单,具体的解决方案:

System.out.println("GCD is: " + gCd( gCd(a, b), c) );

However, if you'll notice, there is recursion going on. 但是,如果您会注意到,则正在进行递归。 I've created a method that takes an array of integers as an input. 我创建了一个将整数数组作为输入的方法。 It will work for an array of size three, or any size. 它适用于大小为3或任何大小的数组。 Here are the methods: 方法如下:

/* returns gcd of two numbers: a and b */
public static int gCd(int a, int b) {
    if (b == 0) {
        return a;
    }
    return gCd(b, a%b);
}

/* returns gcf of an array of numbers */
public static int gCd(int[] numbers)
{
    int result = numbers[0]; // first number

    for(int i = 1; i < numbers.length; i++) {
        result = gCd(result, numbers[i]); // gcf of itself and next #
    }
    return result;
}

So, to relate it to your code: 因此,将其与您的代码相关联:

Scanner userInput = new Scanner(System.in);

System.out.println("Enter first number: ");
int a = userInput.nextInt();

System.out.println("Enter second number: ");
int b = userInput.nextInt();

System.out.println("Enter third number: ");
int c = userInput.nextInt();

// you can do this
System.out.println("GCD is: " + gCd( gCd(a, b), c) );

// or you can do this
int[] numbers = {a, b, c};
int d = gCd(numbers);

System.out.println("GCD is: " + d);

Sample input/output: 样本输入/输出:

Enter first number: 
12
Enter second number: 
18
Enter third number: 
30
GCD is: 6
GCD is: 6

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

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