天天看點

劍指offer--面試題16:數值的整數次方

劍指offer--面試題16:數值的整數次方
#include <iostream>
#include <cmath>


double PowerWithUnsignedExponent(double base, unsigned int exponent)
{
    if (exponent == 0)
        return 1;
    if (exponent == 1)
        return base;

    double result = PowerWithUnsignedExponent(base, exponent >> 1);
    result *= result;
    if ((exponent & 0x1) == 1)
        result *= base;

    return result;
}

int main()
{
    printf("3.0^4=%.2f\n",PowerWithUnsignedExponent(3.0,4));
    return 0;
}

           
劍指offer--面試題16:數值的整數次方

繼續閱讀