文章出處

題目描述
給定一個double類型的浮點數base和int類型的整數exponent。求base的exponent次方。

上次面試讓寫過這個...想要1A的話還是需要熟練些才行。這次寫依然沒考慮全邊緣情況,不過比上次好一些。

class Solution {
public:
    double Power(double base, int exponent) {
        if(exponent==0){
            return 1;
        }
        if(exponent==1){
            return base;
        }
        if(exponent<0){
            return Power(1/base, -exponent);
        }
        if(exponent%2==1){
            return base*Power(base, exponent-1);
        }

        double t = Power(base, exponent/2);
        return t*t;

    }
};

文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()