剑指offer——面试题16:数值的整数次方
// 面试题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:数值的整数次方的更多相关文章
- 剑指Offer:面试题11——数值的整数次方(java实现)
题目描述: 实现函数double Power(double base, int exponent),求base的exponent次方,不得使用库函数,同时不需要考虑大数问题 思路:本题的重点考察内容是 ...
- 剑指Offer - 九度1514 - 数值的整数次方
剑指Offer - 九度1514 - 数值的整数次方2013-11-30 00:49 题目描述: 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponen ...
- 剑指offer_面试题11 数值的整数次方_考察代码的完整性
测试通过代码: package t0825; public class Power { public static void main(String[] args){ System.out.print ...
- 剑指offer(12)数值的整数次方
题目描述 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 题目分析 这道题用传统的方法也可以做,只不过效率太低,这里我们用到快速幂的方法 ...
- 剑指offer十二之数值的整数次方
一.题目 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 二.思路 1.传统方法计算,时间复杂度O(n) 2.递归方式计算,时间复杂度O ...
- 【剑指Offer】12、数值的整数次方
题目描述: 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 解题思路: 本题看似比较简单,是一个简单的指数运算,但需要完 ...
- 【剑指offer】面试题 16. 数值的整数次方
面试题 16. 数值的整数次方 题目描述 题目:给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 解答过程 下面的讨论中 x 代表 bas ...
- 剑指offer 面试题43. 1~n整数中1出现的次数
leetcode上也见过一样的题,当时不会做 看了一下解法是纯数学解法就没看,结果剑指offer上也出现了这道题,那还是认真看下吧 对于数字abcde,如果第一位是1,比如12345,即计算f(123 ...
- C++版 - 剑指offer 面试题16:反转链表(Leetcode 206: Reverse Linked List) 题解
面试题16:反转链表 提交网址: http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId= ...
随机推荐
- 23-tcp协议——TIME_WAIT状态和FIN_WAIT2状态
23-tcp协议——TIME_WAIT状态和FIN_WAIT2状态 摘自:https://blog.csdn.net/qq_35733751/article/details/80146161 2018 ...
- Selenium运用-漫画批量下载
今天我们要爬去的网站是http://comic.sfacg.com/.漫画网站一般都是通过JavaScript和AJAX来动态加载漫画的,这也就意味着想通过原来爬取静态网站的方式去下载漫画是不可能的, ...
- 从jvm运行机制来分析 String对象负值
测试1: 代码 public class Test { public static void main(String[] args) { String s1="aaa"; f(s1 ...
- HDU 4514 湫湫系列故事――设计风景线 (树形DP)
题意:略. 析:首先先判环,如果有环直接输出,用并查集就好,如果没有环,那么就是一棵树,然后最长的就是树的直径,这个题注意少开内存,容易超内存, 还有用C++交用的少一些,我用G++交的卡在32764 ...
- 在win10 + ie11上使用控件
1.1. 在Win10+IE11上提示创建文件错误的问题 解决方法: 1.打开Internet选项 2.取消勾选启用保护模式 选择"不再显示此消息"
- .NET基础 (04)基础类型和语法
基础类型和语法1 .NET中所有内建类型的基类是什么2 System.Object中包含哪些方法,哪些是虚方法3 值类型和引用类型的区别4 简述装箱和拆箱原理5 C#中是否有全局变量6 struct和 ...
- NIOS II 自定义IP核编写基本框架
关于自定义IP .接口 a.全局信号 时钟(Clk),复位(reset_n) b.avalon mm slave 地址(as_address) 片选(as_chipselect /as_chipsel ...
- MySQL的下载和安装
MySQL的下载 MySQL官网:https://www.mysql.com/ MySQL的安装 https://jingyan.baidu.com/article/6181c3e0d27a57152 ...
- C#基础入门 二
C#基础入门 二 循环语句 与C语言中用法相同. continue:结束本次循环(continue)后面的代码不再执行,进入下次循环(通常与if连用). 数组 一维数组定义:int[] intArra ...
- Java50道经典习题-程序1 不死神兔
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少? 分析:假如:1月1日有1对兔子,根据题目“从出生后 ...