题解 | #数值的整数次方#

数值的整数次方

http://www.nowcoder.com/practice/1a834e5e3e1a4b7ba251417554e07c00

  1. 务必注意 注释中的所有列出来的东西。

    class Solution {
    public:
     double Power(double base, int exponent) {
    
         if(exponent==0) return 1.0;
    
         double res = base;//先赋值给一个结果
    
         if(exponent>0){
             for(int i =0 ; i< exponent-1;i++){//减一是一个十分重要的条件
                 res *= base;//注意乘以这个base
             }
         }else{
    
             for(int i =0 ; i< (-exponent)-1;i++){
                 res *= base;
             }
    
             res  = 1/res;
         }
    
         return res;
     }
    };
剑指Offer 文章被收录于专栏

剑指offer的解析结合

全部评论

相关推荐

2025-12-11 14:24
门头沟学院 Java
牛客35720396...:不要用boss,全是骗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务