logexpo.cpp

来自「C++&datastructure书籍源码,以前外教提供现在与大家共享」· C++ 代码 · 共 41 行

CPP
41
字号
#include <iostream>using namespace std;// exponentiation using logarithmic number of multiplications// Owen Astrachandouble Power(double base, int expo)// precondition: expo >= 0     // postcondition: returns base^expo (base to the power expo){    if (0 == expo)    {   return 1.0;                           // correct for zeroth power    }    else    {   double semi = Power(base,expo/2);     // build answer from this                if (expo % 2 == 0)                    // even exponent        {   return semi*semi;        }        else                                  // odd exponent        {   return base*semi*semi;        }           }}int main(){    int expo;    double base,result;        cout << "enter base and exponent: ";    cin >> base >> expo;        result = Power(base,expo);        cout << base << " ** " << expo         << " = " << result << endl;    return 0;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?