[python3] 16 數(shù)值的整數(shù)次方

代碼涉及的python3和python2之間的區(qū)別僅僅在于print函數(shù)是否有括號,在??途W(wǎng)的python2環(huán)境下不需要print任何輸出,因此不影響。

題目描述, 不用庫函數(shù)如何求乘方,以a^n為例
1 若不考慮效率問題,也不考慮錯誤輸入和特殊用例的問題,對a求n次方,即求n次a相乘之后的結(jié)果。
# -*- coding:utf-8 -*-
class Solution:
    def Power(self, base, exponent):
        # write code here
        result = 1
        for i in range(exponent):
            result *= base
        return result
if __name__ == "__main__":
    ss = Solution()
    print (ss.Power(2, 4)) #16
2. 若考慮效率問題,不考慮錯誤輸入和特殊用例的問題,則a^n =(a^(n/2) )* (a^(n/2) ), n為偶數(shù);a^n =(a^((n-1)/2) )*(a^((n-1)/2) ) * a, n為奇數(shù)。這個公式可以采用遞歸的方式實現(xiàn)。遞歸時需要考慮遞歸終止條件,在這里即為n == 1, 若n == 1, 則返回a。
# -*- coding:utf-8 -*-
class Solution:
    def Power(self, base, exponent):
        # write code here
        if exponent == 0:
            return 1
        if exponent == 1:
            return base
        result = self.Power(base, exponent>>1) #exponent>>1相當(dāng)于exponent/2
        result *= result
        if exponent & 1 == 1: #若exponent是奇數(shù),則在上面的基礎(chǔ)上需要再乘以一個base
            result *= base
        return result

if __name__ == "__main__":
    ss = Solution()
    print (ss.Power(2, 5)) #32
3. 若考慮效率問題,同時考慮錯誤輸入和特殊用例的問題。這里的底數(shù)和指數(shù)不一定總是為正數(shù),也可能是負(fù)數(shù)和0. 若底數(shù)為負(fù)數(shù),無論指數(shù)是什么,都和正數(shù)的乘方一樣求得。若底數(shù)為0,指數(shù)為0,則正常計算為1;若底數(shù)為0,指數(shù)為正,正常計算為0;若底數(shù)為0,指數(shù)為負(fù),先求0**(abs(n)), 然后求倒數(shù),0不能作為分母,因此這種情況為錯誤輸入。若底數(shù)為正,指數(shù)為正,則正常計算;若底數(shù)為正,指數(shù)為0,則正常計算為1;若底數(shù)為正,指數(shù)為負(fù),則先計算指數(shù)的絕對值 次冪,然后對計算結(jié)果求倒數(shù)。綜上所述,異常輸入為底數(shù)為0,指數(shù)為負(fù)的情況。
0^0 = 1

對于異常輸入,有三種措施來告知函數(shù)調(diào)用者,你的輸入有問題啦~三種措施分別為返回值,全局變量,和拋出異常。在這里以全局變量返回為例。

# -*- coding:utf-8 -*-
class Solution:
    def Power(self, base, exponent):
        g_InvalidInput = False
        if base == 0 and exponent < 0:
            g_InvalidInput = True #此時需要函數(shù)調(diào)用者自己檢查g_InvalidInput的返回值,來判斷是否輸入異常
            return 0

        absExponent = exponent
        if exponent < 0: #若輸入的指數(shù)為負(fù)數(shù),則先計算指數(shù)為其絕對值時的值,然后計算結(jié)果的倒數(shù)
            absExponent = -exponent
        result = self.PowerWithUnsignedExponent(base, absExponent)

        if exponent < 0:
            result = 1/result
        return result
    
    # 不考慮非法輸入,不考慮指數(shù)為負(fù)數(shù)時,計算乘方
    def PowerWithUnsignedExponent(self, base, exponent):
        # write code here
        if exponent == 0: #任何底數(shù)的0次冪都等于1
            return 1
        if exponent == 1: #遞歸終止條件
            return base
        result = self.PowerWithUnsignedExponent(base, exponent>>1) #exponent>>1相當(dāng)于exponent/2
        result *= result
        if exponent & 1 == 1: #若exponent是奇數(shù),則在上面的基礎(chǔ)上需要再乘以一個base
            result *= base
        return result

if __name__ == "__main__":
    ss = Solution()
    g_InvalidInput = False
    base = 2
    exponent = -5
    result = ss.Power(base, exponent)
    if g_InvalidInput == True:
        print ("Invalid Input!!!")
    print ("%s ^ %s = %s" %(base, exponent, result))
正確運行
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容