// 面试题16:数值的整数次方
// 题目:实现函数double Power(double base, int exponent),求base的exponent
// 次方。不得使用库函数,同时不需要考虑大数问题。 #include <iostream>
#include <cmath> bool g_InvalidInput = false;
bool equal(double num1, double num2);
double PowerWithUnsignedExponent(double base, unsigned int exponent); double Power(double base, int exponent)
{
g_InvalidInput = false; if (equal(base, 0.0) && exponent < )
{
g_InvalidInput = true;
return 0.0;
} unsigned int absExponent = (unsigned int) (exponent);
if (exponent < )
absExponent = (unsigned int) (-exponent); double result = PowerWithUnsignedExponent(base, absExponent);
if (exponent < )
result = 1.0 / result; return result;
} /*
double PowerWithUnsignedExponent(double base, unsigned int exponent)
{
double result = 1.0; for (int i = 1; i <= exponent; ++i)
result *= base;
return result;
}
*/ double PowerWithUnsignedExponent(double base, unsigned int exponent)
{
if (exponent == )
return ;
if (exponent == )
return base; double result = PowerWithUnsignedExponent(base, exponent >> );
result *= result;
if ((exponent & 0x1) == )
result *= base; return result;
} bool equal(double num1, double num2)
{
if ((num1 - num2 > -0.0000001) && (num1 - num2 < 0.0000001))
return true;
else
return false;
} // ====================测试代码====================
void Test(const char* testName, double base, int exponent, double expectedResult, bool expectedFlag)
{
double result = Power(base, exponent);
if (equal(result, expectedResult) && g_InvalidInput == expectedFlag)
std::cout << testName << " passed" << std::endl;
else
std::cout << testName << " FAILED" << std::endl;
} int main(int argc, char* argv[])
{
// 底数、指数都为正数
Test("Test1", , , , false); // 底数为负数、指数为正数
Test("Test2", -, , -, false); // 指数为负数
Test("Test3", , -, 0.125, false); // 指数为0
Test("Test4", , , , false); // 底数、指数都为0
Test("Test5", , , , false); // 底数为0、指数为正数
Test("Test6", , , , false); // 底数为0、指数为负数
Test("Test7", , -, , true); return ;
}

本人答案:

 #include"iostream"
#include"stdio.h"
#include"stdexcept"
using namespace std; bool errorFlag=false; double GetPower(double base,int exponent)
{
if(exponent==)
return 1.0;
if(exponent==)
return base; double result=GetPower(base,exponent>>);
result*=result;
if(exponent&)
result*=base;
return result;
} bool Equal(double num1,double num2)
{
if((num1-num2)>-0.000001&&(num1-num2)<0.000001)
return true;
return false;
} double Power(double base,int exponent)
{
errorFlag=false;
double result=;
// cout<<exponent<<endl;
if(Equal(base,)&&exponent<)
{
errorFlag=true;
return ;
}
if(exponent<)
{
result=1.0/GetPower(base,-exponent);
}
else
{
result=GetPower(base,exponent);
}
return result;
} void Test(char *testName,double base,int exponent,double expectResult,bool expectFlag)
{
//注意这里需要后判断flag,不然前面函数没执行,errorFlag值不会得到更新
if(Equal(Power(base,exponent),expectResult)&&errorFlag==expectFlag)
{
cout<<testName<<":passed."<<endl;
}
else
{
cout<<testName<<":failed."<<endl;
}
} int main()
{
Test("Test1",-,-,0.25,false);
Test("Test2",-,,-,false);
Test("Test3",-,,,false);
Test("Test4",,-,,true);
Test("Test5",,,,false);
Test("Test6",,,,false);
Test("Test7",,,,false);
Test("Test8",,-,0.5,false);
Test("Test9",,,,false);
return ;
}

剑指offer——面试题16:数值的整数次方的更多相关文章

  1. 剑指Offer:面试题11——数值的整数次方(java实现)

    题目描述: 实现函数double Power(double base, int exponent),求base的exponent次方,不得使用库函数,同时不需要考虑大数问题 思路:本题的重点考察内容是 ...

  2. 剑指Offer - 九度1514 - 数值的整数次方

    剑指Offer - 九度1514 - 数值的整数次方2013-11-30 00:49 题目描述: 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponen ...

  3. 剑指offer_面试题11 数值的整数次方_考察代码的完整性

    测试通过代码: package t0825; public class Power { public static void main(String[] args){ System.out.print ...

  4. 剑指offer(12)数值的整数次方

    题目描述 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 题目分析 这道题用传统的方法也可以做,只不过效率太低,这里我们用到快速幂的方法 ...

  5. 剑指offer十二之数值的整数次方

    一.题目 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 二.思路 1.传统方法计算,时间复杂度O(n) 2.递归方式计算,时间复杂度O ...

  6. 【剑指Offer】12、数值的整数次方

      题目描述:   给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方.   解题思路:   本题看似比较简单,是一个简单的指数运算,但需要完 ...

  7. 【剑指offer】面试题 16. 数值的整数次方

    面试题 16. 数值的整数次方 题目描述 题目:给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 解答过程 下面的讨论中 x 代表 bas ...

  8. 剑指offer 面试题43. 1~n整数中1出现的次数

    leetcode上也见过一样的题,当时不会做 看了一下解法是纯数学解法就没看,结果剑指offer上也出现了这道题,那还是认真看下吧 对于数字abcde,如果第一位是1,比如12345,即计算f(123 ...

  9. C++版 - 剑指offer 面试题16:反转链表(Leetcode 206: Reverse Linked List) 题解

    面试题16:反转链表 提交网址: http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId= ...

随机推荐

  1. Transferring Data Between ASP.NET Web Pages

    14 July 2012 20:24 http://www.mikesdotnetting.com/article/192/transferring-data-between-asp-net-web- ...

  2. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'deptDao_a' defined in class path resource [beansAndHibernate.xml]: Cannot resolve reference to bean 'sessionFact

    Error creating bean with name 'deptDao_a' defined in class path 因为更改了类的名字,所以其setter方法没有更改,需要 private ...

  3. Python基于VS2013 开发环境搭建 Hello World 10分钟搞定

    1.先下载Python 安装 Next ->安装完成 2.以前安装过VS2013 打开VS2013 文件->新建项目 (此时如果没有Python Application,请点击里面的安装插 ...

  4. c#范型

    泛型介绍:范型类和范型方法同事具备可重用性.类型安全和效率,这是非范型类和非范型方法无法具备的. 所谓范型,即通过参数化类型实现同一份代码上操作多种数据类型,范型编程是一种编程范式,它利用“参数化类型 ...

  5. SpringMVC源码解读 - RequestMapping注解实现解读 - ConsumesRequestCondition

    consumes  指定处理请求的提交内容类型(media-Type),例如application/json, text/html. 所以这边的ConsumesRequestCondition就是通过 ...

  6. SpringMVC源码解读 - HandlerMapping - SimpleUrlHandlerMapping初始化

    摘要: SimpleUrlHandlerMapping只是参与Handler的注册,请求映射时由AbstractUrlHandlerMapping搞定. 初始化时,通过setMappings(Prop ...

  7. CodeForces 834D The Bakery(线段树优化DP)

    Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...

  8. PyCharm可用Active Code分享

    目前可用,不保证更新!请及时取用. 6YQUPH9R7H-eyJsaWNlbnNlSWQiOiI2WVFVUEg5UjdIIiwibGljZW5zZWVOYW1lIjoi5o6I5p2D5Luj55C ...

  9. CentOS7安装confluenceWIKI并破解汉化

    关闭防火墙和selinux   开始搭建Wiki前,需要下载一些软件包. wget https://www.atlassian.com/software/confluence/downloads/bi ...

  10. RobotFramework的Setup和Teardown

    测试套件级别的Setup会在本套件测试用例集合执行前先执行,同理Teardown会在本组所有用例执行完成后运行 测试用例级别的Setup会在本条测试用例执行前先执行,同理Teardown会在本条用例执 ...