2012-12-02 57 views
9

這裏是問題:如何編寫一個簡單的Java程序,在兩個數字之間找到最大公約數?

「寫一個方法名爲GCD接受兩個整數作爲參數和返回兩個數的最大公約數兩個整數a的最大公約數(GCD)和B是最大的整數是a和b的因數任何數字和1的GCD是1,任何數字和0的GCD是該數字

計算兩個數字的GCD的一種有效方式是使用歐幾里德的算法,其狀態如下:

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

我真的很困惑, w解決這個問題。我只想提供一些提示和提示,以瞭解到目前爲止我在程序中做了什麼錯誤。 (我必須放入掃描儀,這是我老師的要求。) 不要給我一個完整的代碼,因爲我有點想自己解決這個問題。也許只是給我一個提示,我如何將這個公式結合到上面。 (如果你想知道爲什麼我放入== 0,這是因爲我認爲如果你有兩個數字,比如說0和90,他們的GCD就是0吧?)

另外,我的代碼有以包括while循環...我會更喜歡如果循環...

在此先感謝! :)

我目前的計劃:

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

提示 - 您需要遞歸調用。 – SergeyS

+1

您將'a'和'b'作爲參數傳遞給您的方法,因此不需要從控制檯再次讀取它們(並且'console'變量在方法中不可見,所以您的代碼無法編譯) 。另外,如果輸入,第一個循環看起來非常無限。 – jlordo

+1

*我會更喜歡如果循環... *這是相當複雜的,因爲如果沒有循環 – zapl

回答

28

遞歸方法是:

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.

+2

我認爲你的代碼假設a> b,如果有人打電話給gcd(2,20),它將不起作用。 – Patrick

+5

@Patrick它會的。在下一步中,它會調用gcd(20,2) – Rushil

0

一種方式來做到這一點是下面的代碼:

 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。

1
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; 

} 
} 
0
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); 
} 
2
public static int GCD(int x, int y) { 
    int r; 
    while (y!=0) { 
     r = x%y; 
     x = y; 
     y = r; 
    } 
    return x; 
} 
8

你也可以做到在一個三線法:

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

這裏,如果y = 0,返回x。否則,將再次調用gcd方法,並使用不同的參數值。現在

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

,我剛開始編程大約一個星期前,所以沒有什麼花哨,但我有這是一個問題以及與此上來,這可能是人誰是剛剛進入編程更容易理解。它像前面的例子一樣使用Euclid的方法。

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); 
    } 
} 
相關問題