【问题标题】:How to write a simple Java program that finds the greatest common divisor between two numbers? [duplicate]如何编写一个简单的 Java 程序来找到两个数字之间的最大公约数? [复制]
【发布时间】:2012-12-02 20:37:07
【问题描述】:

问题来了:

"编写一个名为 gcd 的方法,它接受两个整数作为参数并返回这两个数的最大公约数。两个整数 a 和 b 的最大公约数 (GCD) 是同时是 a 的因数的最大整数b. 任意数与1的GCD为1,任意数与0的GCD为该数。

计算两个数的 GCD 的一种有效方法是使用 Euclid 算法,该算法说明如下:

GCD(A, B) = GCD(B, A % B) 
GCD(A, 0) = Absolute value of A"

我真的很困惑如何解决这个问题。我只是想要一些关于我在迄今为止的程序中做错了什么的提示和技巧。 (我必须放一个扫描仪,这是我老师的要求。) 不要给我完整的代码,因为我有点想自己解决这个问题。也许只是给我一个提示,说明我如何结合您在上面看到的这个公式。 (如果你想知道我为什么输入 == 0,那是因为我认为如果你有两个数字,比如 0 和 90,它们的 GCD 应该是 0 对吧??)

另外,我的代码必须包含 while 循环...我更喜欢 if 循环...

提前致谢! :)

我目前的计划:

public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        int a = console.nextInt();
        int b = console.nextInt();
        gcd (a, b);
    }

    public static void gcd(int a, int b) {
        System.out.print("Type in two numbers and I will print outs its Greatest Common Divisor: ");
        int gcdNum1 = console.nextInt();
        int gcdNum2 = console.nextInt();
        while (gcdNum1 == 0) {
            gcdNum1 = 0;
        }
        while (gcdNum2 > gcdNum1) {
            int gcd = gcdNum1 % gcdNum2;
        }
        System.out.print(gcdNum1 + gcdNum2);
    }
}

【问题讨论】:

  • 提示 - 你需要一个递归调用。
  • 您将ab 作为参数传递给您的方法,因此无需从控制台再次读取它们(并且console 变量在方法中不可见,因此您的代码无论如何都不会编译)。此外,如果输入,第一个循环看起来非常无限。
  • 我更喜欢 if 循环...这很复杂,因为 if 没有循环
  • 哎呀,你是对的。哈哈错字了。

标签: java loops while-loop greatest-common-divisor


【解决方案1】:

递归方法是:

static int gcd(int a, int b)
{
  if(a == 0 || b == 0) return a+b; // base case
  return gcd(b,a%b);
}

使用while循环:

static int gcd(int a, int b)
{
  while(a!=0 && b!=0) // until either one of them is 0
  {
     int c = b;
     b = a%b;
     a = c;
  }
  return a+b; // either one is 0, so return the non-zero value
}

当我返回 a+b 时,我实际上返回的是非零数,假设其中一个为 0。

【讨论】:

  • 我认为你的代码假设 a > b,如果有人调用 gcd(2, 20),它就不起作用了。
  • @Patrick 会的。在下一步中,它将调用 gcd(20,2)
【解决方案2】:

你也可以用三行方法:

public static int gcd(int x, int y){
  return (y == 0) ? x : gcd(y, x % y);
}

这里,如果y = 0,则返回x。否则,将再次调用gcd 方法,使用不同的参数值。

【讨论】:

    【解决方案3】:
    public static int GCD(int x, int y) {   
        int r;
        while (y!=0) {
            r = x%y;
            x = y;
            y = r;
        }
        return x;
    }
    

    【讨论】:

      【解决方案4】:
      import java.util.Scanner;
      
      
      public class Main {
      
      
      
      
      public static void  main(String [] args)
      {
          Scanner input = new Scanner(System.in);
          System.out.println("Please enter the first integer:");
          int b = input.nextInt();
          System.out.println("Please enter the second integer:");
          int d = input.nextInt();
      
          System.out.println("The GCD of " + b + " and " + d + " is " +  getGcd(b,d) + ".");
      }
      
      
      public static int getGcd(int b, int d)
      {
          int gcd = 1;
      
          if(b>d)
          {
              for(int i = d; i >=1; i--)
              {
                  if(b%i==0 && d%i ==0)
                  {
                      return i;
                  }
              }
          }
          else
          {
              for(int j = b; j >=1; j--)
              {
                  if(b%j==0 && d% j==0)
                  {
                      return j;
                  }
              }
          }   
          return gcd;
      
      }
      }
      

      【讨论】:

        【解决方案5】:

        一种方法是下面的代码:

                int gcd = 0;
                while (gcdNum2 !=0 && gcdNum1 != 0 ) {
                if(gcdNum1 % gcdNum2 == 0){
                    gcd = gcdNum2;
                }
                    int aux = gcdNum2; 
                    gcdNum2 = gcdNum1 % gcdNum2;
                    gcdNum1 = aux;
            }
        

        您不需要递归来执行此操作。

        并且要小心,它说当一个数字为零时,GCD 就是不为零的数字。

            while (gcdNum1 == 0) {
            gcdNum1 = 0;
        }
        

        您应该修改它以满足要求。

        我不会告诉你如何完全修改你的代码,只会告诉你如何计算 gcd。

        【讨论】:

          【解决方案6】:
          private static void GCD(int a, int b) {
          
              int temp;
              // make a greater than b
              if (b > a) {
                   temp = a;
                   a = b;
                   b = temp;
              }
          
              while (b !=0) {
                  // gcd of b and a%b
                  temp = a%b;
                  // always make a greater than bf
                  a =b;
                  b =temp;
          
              }
              System.out.println(a);
          }
          

          【讨论】:

            【解决方案7】:
            import java.util.Scanner;
            
            class CalculateGCD 
            {   
              public static int calGCD(int a, int b) 
              { 
               int c=0,d=0;  
               if(a>b){c=b;} 
               else{c=a;}  
               for(int i=c; i>0; i--) 
               { 
                if(((a%i)+(b%i))==0) 
                { 
                 d=i; 
                 break; 
                } 
               } 
               return d;  
              }  
            
              public static void main(String args[]) 
              { 
               Scanner sc=new Scanner(System.in); 
               System.out.println("Enter the nos whose GCD is to be calculated:"); 
               int a=sc.nextInt(); 
               int b=sc.nextInt(); 
               System.out.println(calGCD(a,b));  
              } 
             } 
            

            【讨论】:

              【解决方案8】:

              现在,我大约一周前才开始编程,所以没什么特别的,但我遇到了这个问题并想出了这个,这对于刚开始编程的人来说可能更容易理解。它像前面的例子一样使用欧几里得的方法。

              public class GCD {
                public static void main(String[] args){
                  int x = Math.max(Integer.parseInt(args[0]),Integer.parseInt(args[1]));    
                  int y = Math.min(Integer.parseInt(args[0]),Integer.parseInt(args[1]));     
                  for (int r = x % y; r != 0; r = x % y){
                    x = y;
                    y = r;
                  }
                  System.out.println(y);
                }
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2022-10-24
                • 1970-01-01
                • 1970-01-01
                • 2022-12-07
                • 2020-04-17
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多