溫馨提示×

python怎么實現(xiàn)冪函數(shù)

九三
1016
2021-02-18 18:34:00
欄目: 編程語言

python怎么實現(xiàn)冪函數(shù)

在python中利用遞歸實現(xiàn)一個冪函數(shù),具體方法如下:

double PowerWithExponentUnsigned(double base, unsigned int exponentUnsigned)

{

// 最小子問題

if(exponentUnsigned == 0)

return 1;

double result = PowerWithExponentUnsigned(base,exponentUnsigned / 2);

result = result * result;

if(exponentUnsigned % 2 == 1)

{

result *= base;

}

return result;

}

0