數值的整數次方

2021-08-21 19:31:43 字數 956 閱讀 6854

給定乙個double型別的浮點數base和int型別的整數exponent。求base的exponent次方。

題解:這道題,讓我學習到了快速冪的用法。。雖然還不熟練,還有&和左移右移

貼一下別人的解,c++的:

/**

* 1.全面考察指數的正負、底數是否為零等情況。

* 2.寫出指數的二進位制表達,例如13表達為二進位制1101。

* 3.舉例:10^1101 = 10^0001*10^0100*10^1000。

* 4.通過&1和》1來逐位讀取1101,為1時將該位代表的乘數累乘到最終結果。

*/

publicdoublepower(doublebase,intn)elseif(n<0)else

while(exponent!=0)

returnn>=0?res:(1/res);

}

這種的時間複雜度為o(logn)

數值整數次方

題目 實現函式double power double base,int exponent 求base的exponent次方。不得使用庫函式,同時不需要考慮 大數問題。includebool equal double num1,double num2 double powerwithunsignede...

數值整數次方

題目 實現函式double power double base,int exponent 求base的exponent次方。不得使用庫函式,同時不需要考慮 大數問題。includebool equal double num1,double num2 double powerwithunsignede...

數值的整數次方

題目 實現函式double power double base,int exponent 求base的exponent次方。不得使用庫函式,同時不需要考慮大樹問題。這道題目有以下幾點需要注意 0的0次方是無意義的,非法輸入 0的負數次方相當於0作為除數,也是無意義的,非法輸入 base如果非0,如果...