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);
}
}
我所要的輸出看起來像下面的圖片:
。
爲什麼人們認爲這個問題表明研究工作?堆棧溢出不是外包作業的正確地方。 –
猜猜OP的下列問題是:[我如何計算4個數字的GCD](http://stackoverflow.com/questions/42432503/compute-the-greatest-common-divisor-of-four-numbers)大聲笑任何人想猜測OP的下一個問題是什麼? –