2017-02-23 97 views
0

用三個數字找到GCD(最大共同分頻器)的方法是什麼?
下面的代碼顯示了2個數字的方法,它使用歐幾里德算法的基本版本(因爲輸入是正數)來計算GCD。什麼是計算3個數字GCD的方法

public class GCD { 
public static void main(String[] args) { 

    int age1 = 10; 
    int age2 = 15; 

    int multiple1OfGCD = age1; 
    int multiple2OfGCD = age2; 



    while (multiple1OfGCD != multiple2OfGCD) { 

    if (multiple1OfGCD > multiple2OfGCD) { 
    multiple1OfGCD -= multiple2OfGCD; 
    } 
    else { 
    multiple2OfGCD -= multiple1OfGCD; 
    } 
    } 

    System.out.println("The GCD of " + age1 + " and " + age2 + " is " + multiple1OfGCD); 


    int noOfPortions1 = age1/multiple1OfGCD; 
    int noOfPortions2 = age2/multiple1OfGCD; 




    System.out.println("So the cake should be divided into " 

    + (noOfPortions1 + noOfPortions2)); 

    System.out.println("The " + age1 + " year old gets " + noOfPortions1 

    + " and the " + age2 + " year old gets " + noOfPortions2); 

} 
} 

我所要的輸出看起來像下面的圖片:

Want the output to look like this

+0

爲什麼人們認爲這個問題表明研究工作?堆棧溢出不是外包作業的正確地方。 –

+0

猜猜OP的下列問題是:[我如何計算4個數字的GCD](http://stackoverflow.com/questions/42432503/compute-the-greatest-common-divisor-of-four-numbers)大聲笑任何人想猜測OP的下一個問題是什麼? –

回答

0

你在正確的軌道我修改你的代碼一點點就稍微更容易閱讀:

class GCD { 
    public static void main(String[] args) { 
    final int AGE1 = 10; 
    final int AGE2 = 15; 
    final int AGE3 = 20; 
    getGCDAndCutCake(AGE1, AGE2, AGE3); 
    } 

    public static int gcd(int a, int b) { 
    if(a == 0) return b; 
    if(b == 0) return a; 
    if(a > b) return gcd(b, a % b); 
    return gcd(a, b % a); 
    } 

    public static void getGCDAndCutCake(int age1, int age2, int age3) { 
    int gcdOf1and2 = gcd(age1, age2); 
    int overallGCD = gcd(gcdOf1and2, age3); 
    int pieces = (age1 + age2 + age3)/overallGCD; 
    System.out.printf("The overall GCD of %d, %d and %d is %d\n", age1, age2, age3, overallGCD); 
    System.out.printf("So the cake should be divided into %d\n", pieces); 
    System.out.printf("The %d year old gets %d\n", age1, age1/overallGCD); 
    System.out.printf("The %d year old gets %d\n", age2, age2/overallGCD); 
    System.out.printf("The %d year old gets %d\n", age3, age3/overallGCD); 
    } 
} 

輸出:

The overall GCD of 10, 15 and 20 is 5 
So the cake should be divided into 9 
The 10 year old gets 2 
The 15 year old gets 3 
The 20 year old gets 4 

試試吧here!

0

希望它會幫助

public static void main (String[] args) 
{ 
int a,b,c; 
a=10; 
b=15; 
c=20; 
int d= gcd(a,b,c); 
System.out.println("The GCD of "+a+", "+b+" and "+c+ " is "+d); 
int cake=a/d+b/d+c/d; 
System.out.println("So the cake is divided into "+ cake); 
System.out.println("The "+a+ " Years old get "+a/d); 
System.out.println("The "+b+ " Years old get "+b/d); 
System.out.println("The "+c+ " Years old get "+c/d); 
} 

public static int gcd(int a, int b, int c){ 
return calculateGcd(calculateGcd(a, b), c); 
} 

public static int calculateGcd(int a, int b) { 
    if (a == 0) return b; 
    if (b == 0) return a; 
    if (a > b) return calculateGcd(b, a % b); 
    return calculateGcd(a, b % a); 
} 
} 
相關問題