2017-03-28 43 views
1

我最近一直在尋找遞歸,這一直很混亂。我正在嘗試編寫一個java程序,它使用遞歸來生成一個基數的結果給一個冪,該冪的基數也有多少個指數。例如,兩個具有3的功率和的3高度的鹼會導致:如何使用遞歸計算功率的功率?

(((2^3)^ 3)^ 3)

這是我的(非常有缺陷)代碼:

public static void main(String[] args) { 
    System.out.println(exponentWithHeight(2, 2, 3)); 
} 

public static int exponentWithHeight(int base, int power, int height) { 
    if (power < 0 || height < 0) { 
     throw new IllegalArgumentException("Check input values."); 
    } 
    if (power == 0) { 
     return 1; 
    } else { 
     return (base * height * exponentWithHeight(base, power - 1, height - 1)); 
    } 
} 

我真的不知道如何去得到我想要的輸出;正如我所說的,這對我來說真是令人困惑和新鮮。有人可以幫助解釋如何去做這件事嗎?

+1

什麼是你期望的輸出,並且您能得到什麼呢?你在調試時發現了什麼? –

+0

因此,當我使用調用方法exponentWithHeight(2,2,3)時,我的期望輸出將是256.相反,我得到24.當我調試它時,一切都運行完美。 –

+0

「一切都運行完美」你是什麼意思?你調試它時會得到256嗎?或者你沒有得到任何錯誤? (後者是毫無意義的,如果你沒有得到正確的結果,你顯然會有錯誤) –

回答

1

這是一個雙重遞歸,無論是身高還是力量。

請注意,如果將值緩存在某處是有意義的,因爲exponentWithHeight(base,power,0)將被重新計算多次。

結果與此代碼是256:

public static void main(String[] args) { 
    System.out.println(exponentWithHeight(2, 2, 3)); 
} 

public static int exponentWithHeight(int base, int power, int height) { 
    if (power < 0 || height < 0) { 
     throw new IllegalArgumentException("Invalid Input; Check power and/or height value."); 
    } 
    if (base == 0) { 
     return 0; 
    } 
    if (height == 0) { 
     return calcPower(base, power); 
    } else { 
     return exponentWithHeight(calcPower(base, power), power, height - 1); 
    } 
} 

public static int calcPower(int base, int power) { 
    if (power == 0) { 
     return 1; 
    } 
    return base * power(base, power - 1); 
} 
+0

看起來好像這段代碼不能正確計算值;當我輸入(3,2,2),而不是得到81作爲答案,我得到729.當你乘[exponentWithHeight(base,power,0)* exponentWithHeight(base,power,height - 1) ]?編輯它 –

+0

。獲得第一個基地和權力的價值。並從功率中減去1。 –

0

難道我們忘了,你可以乘一系列指數的共同簡化方程?

以下應該工作。

public static int exponentWithHeight(int base, int power, int height) { 
    if (power < 0 || height < 0) { 
     throw new IllegalArgumentException("Invalid Input; Check power and/or height value."); 
    } 
    if (power == 0 || height == 0) { 
     return 1; 
    } else { 
     return (int) Math.pow(base, Math.pow(power, height)); 
    } 
} 
+1

除了'Math.pow'返回'double'而不是'int',我認爲OP的練習點不是使用'Math.pow'。 –

+0

@AndyTurner OP從未指定他們不希望使用遞歸。 – Qix

+2

標題指定OP *不希望(/需要)使用遞歸:「如何使用遞歸計算功率的功率?」 –

1

這裏是另一個簡單明瞭爲例​​使用for循環來計算電源,然後計算高度使用遞歸

public static long exponentWithHeight(long base, long power, long height) { 
      long calculatedPower; 
      if (power < 0 || height < 0) { 
       throw new IllegalArgumentException("Invalid Input; Check power and/or height value."); 
      } 
      if (power == 0) { 
       return 1; 
      } 
      if (height > 0) { 
       calculatedPower = base; 
       for (int i = (int) power; i > 1; i--) { 
         calculatedPower *= base; 
       } 
       return exponentWithHeight(calculatedPower, power, --height); 
      } 

      return base; 

    } 
+0

謝謝!這很容易閱讀,像魅力一樣工作。 –

+0

不客氣。但你應該**請記住**,這個代碼只適用於小功率和高度數字,因爲如果你使用一些大的數字來表示功率或者像4或者更高的高度,那麼計算的數字將會超過'long'數據類型的容量最大值,它將返回0。 –