首页 > 代码库 > 剑指offer---数值的整数次方

剑指offer---数值的整数次方

class Solution
{
public:
    double Power(double base, int exponent)
    {
        if (exponent > 0)
        {
            double sqr = 1;
            int i = 0;
            for (int i = 0; i < exponent; ++i)
            {
                sqr = sqr*base;
            }
            return sqr;
        }
        else if (exponent == 0) return 1;
        else
        {
            int zhengshu = abs(exponent);
            double sqr = 1;
            int i = 0;
            for (int i = 0; i < zhengshu; ++i)
            {
                sqr = sqr*base;
            }
            return (1/sqr);
        }

    }
};

 

剑指offer---数值的整数次方