AcWing 27. 数值的整数次方

地址 https://www.acwing.com/problem/content/description/26/

 

题目描述
实现函数double Power(double base, int exponent),求base的 exponent次方。

不得使用库函数,同时不需要考虑大数问题。

注意:

不会出现底数和指数同为0的情况

样例

输入:102

输出:100

输入:10 ,-2

输出:0.01

算法1
按照题意来 注意指数的正负

C++ 代码

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

        return res;
    }
};

作者:defddr
链接:https://www.acwing.com/solution/acwing/content/3327/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

posted on 2019-08-03 00:06  itdef  阅读(116)  评论(0编辑  收藏  举报

导航