zoukankan      html  css  js  c++  java
  • 【算法24】幂函数的实现

    【题  目】实现函数double Power(double base,int exponent),求base的exponent次方,不需要考虑溢出。

    【思  路】这道题的核心太简单了,一个循环就搞定,就不在多说了。关键是我们要考虑代码的健壮性:(1)首先base=0,exponent=0在数学上是无意义的;base=0,exponent<0的时候是分母为零的情况,我们要作为特殊情况考虑。(2)如果exponent为负数,那么我们要首先求得对应的正数的幂,然后再取倒数。

      这样我们就可以得到如下的代码:

     1 #include<iostream>
    2 #include<string>
    3 using namespace std;
    4
    5 bool InvalidInput = false;
    6 double PowerWithExponentUnsigned(double base, unsigned int exponentUnsigned);
    7
    8 /**************************************************************************
    9 * 计算base的exponent次幂
    10 **************************************************************************/
    11 double Power(double base,int exponent)
    12 {
    13 InvalidInput = false;
    14
    15 //底数等于0,expont小于等于0,无效输入
    16 if(base == 0.0 && exponent <= 0)
    17 {
    18 InvalidInput = true;
    19 return 0.0;
    20 }
    21
    22 //将指数一律转换成正数计算
    23 unsigned int exponentUnsigned = static_cast<unsigned int>(exponent);
    24 if(exponent < 0)
    25 {
    26 exponentUnsigned = static_cast<unsigned int>(-exponent);
    27 }
    28
    29 //计算正指数时的结果
    30 double result = PowerWithExponentUnsigned(base,exponentUnsigned);
    31
    32 //如果指数为负数,求倒数
    33 if(exponent < 0)
    34 result = 1.0/result;
    35
    36 return result;
    37 }
    38
    39
    40 /************************************************************************
    41 * 指数为正,计算base的exponentUnsigned次幂
    42 ************************************************************************/
    43 double PowerWithExponentUnsigned(double base, unsigned int exponentUnsigned)
    44 {
    45 double result = 1.0;
    46 for(unsigned int i = 1;i <= exponentUnsigned;i++)
    47 {
    48 result *= base;
    49 }
    50 return result;
    51 }
    52
    53
    54 int main()
    55 {
    56 cout<<"please enter your base and your expont:"<<endl;
    57 double bas = 0.0;
    58 int expont = 0;
    59 cin>>bas>>expont;
    60
    61 cout<<"the result is:"<<endl;
    62 cout<<Power(bas,expont)<<endl;
    63
    64 return 0;
    65 }

      运行结果如下:

      上述算法考虑到情况已经比较全面了,但是还有一点需要改进,我们的子函数需要exponent次乘法,其实我们对于乘方的算法有如下的公式:

      从上面的公式我们可以看出只需要logn次乘法就可以了,这是一个简单的递归式,由此我们可以改进子函数如下:

    /************************************************************************
    * 指数为正,计算base的exponentUnsigned次幂
    ***********************************************************************
    */
    double PowerWithExponentUnsigned(double base, unsigned int exponentUnsigned)
    {

    // 最小子问题
    if(exponentUnsigned == 0)
    return 1;

    double result = PowerWithExponentUnsigned(base,exponentUnsigned / 2);
    result = result * result;

    if(exponentUnsigned % 2 == 1)
    {
    result *= base;
    }

    return result;
    }

       只需要将第一次的源代码中的子函数就这个子函数替换就可以进行测试,笔者测试结果如下:


    References:

    程序员面试题精选100题:http://zhedahht.blog.163.com/blog/static/254111742009101563242535/

    注:

    1)本博客所有的代码环境编译均为win7+VC6。所有代码均经过博主上机调试。

    2)博主python27对本博客文章享有版权,网络转载请注明出处http://www.cnblogs.com/python27/。对解题思路有任何建议,欢迎在评论中告知。

  • 相关阅读:
    [读书笔记]Applying UML and patterns:The agile manifesto and principles
    关于CheckBoxList和RadioButtonList的几个问题
    教你背单词
    深入剖析引用参数Ref和Out
    Web的系统测试方法 (转载)
    .net Compact Framework 程序设计起步(智能设备的程序设计)
    知道Ping的最后一个返回值TTL是什么意思吗?
    精明人的四个等级[转]
    HTTP协议下用Web Service上传大文件的解决方案
    如何解决DataGrid中删除记录后分页错误
  • 原文地址:https://www.cnblogs.com/python27/p/2291372.html
Copyright © 2011-2022 走看看