《剑指offer》:自己写一个pow函数
来源:互联网 发布:linux proccess port 编辑:程序博客网 时间:2024/04/29 15:32
缘由
今天打google在codejam上办的apactest,成绩还行吧(最后排名540),第二题逗比了,自己逻辑后来理清楚,但是代码还是原来的想法,wa了两次才找到了bug。
第二题需要实现一个整数的pow函数,之前只会递归的写法,今天学会了迭代的写法,回想《剑指offer》里也有类似的东西,所以整理成这篇博客。
整数的非负整数次幂(不考虑大数)
线性求幂
比如求2的10次幂,可能有人随手十几秒就写完了:
typedef long long LL;LL pow(int base, int exponent) { LL ans = 1; for (int i = 0; i < exponent; ++i) { ans *= base; } return ans;}
如果exponent是比较大的呢?有没有更快的?
快速幂算法登场
我们把exponent表示成二进制的形式,比如15=1111,那么其实有:
这样计算的话,我们只需要计算
那么代码怎么写呢?
typedef long long LL;LL pow(int base, int exponent) { LL ans = 1, last_pow = base; while (exponent > 0) { // 如果最低位为1 if ((exponent & 1) != 0) ans = ans * last_pow ; exponent = exponent >> 1; last_pow = last_pow * last_pow; } return ans;}
其实我觉得代码已经很清楚了,如果还不清楚的话,可以稍微解释一下(请先自行熟悉位运算):
假如我们使用上面的函数来迭代计算
第一步:发现1011的最低位为1,ans乘上
第二步:发现101的最低位为1,ans乘以
第三步,发现10的最低位不为1,ans不变,10右移一位变成1;
第四步,发现1的最低位为1,ans乘以
注意每一步里的
整数次幂
注意现在的问题变成了整数次幂了,也就是包括负数次幂了!
首先回顾一下幂运算的定义:
也就是说,对于负数次幂,其实只要计算它的绝对值次幂,再用1去除就可以了,还要注意检查计算0的0次幂这种非法情况!
typedef double NumberType;// 计算非负整数次幂的函数NumberType powWithoutNegativeExp(NumberType base, int exponent) { NumberType ans = 1, last_pow = base; while (exponent > 0) { if ((exponent & 1) != 0) ans = ans * last_pow ; exponent = exponent >> 1; last_pow = last_pow * last_pow; } return ans;}// 浮点数相等的判断比较特别bool equalD(NumberType numA, NumberType numB) { static const NumberType ERROR = 1e-12; return (numA - numB <= ERROR && numA - numB >= -ERROR);}// 处理各种情况的幂运算的函数NumberType pow(NumberType base, int exponent) { // 0的0次幂没有意义,抛出异常 if (exponent == 0 && equalD(base, 0)) { throw logic_error("Zero's zero exponent is undefine."); } bool isNegative = false; if (exponent < 0) { isNegative = true; exponent = -exponent; } NumberType result = powWithoutNegativeExp(base, exponent); return isNegative ? 1 / result : result;}
注释代码里有了~应该是很清晰的。
另附上完整的使用示例:
#include <iostream>#include <stdexcept>using namespace std;typedef double NumberType;NumberType powWithoutNegativeExp(NumberType base, int exponent) { NumberType ans = 1, last_pow = base; while (exponent > 0) { if ((exponent & 1) != 0) ans = ans * last_pow ; exponent = exponent >> 1; last_pow = last_pow * last_pow; } return ans;}bool equalD(NumberType numA, NumberType numB) { static const NumberType ERROR = 1e-12; return (numA - numB <= ERROR && numA - numB >= -ERROR);}NumberType pow(NumberType base, int exponent) { if (exponent == 0 && equalD(base, 0)) { throw logic_error("Zero's zero exponent is undefine."); } bool isNegative = false; if (exponent < 0) { isNegative = true; exponent = -exponent; } NumberType result = powWithoutNegativeExp(base, exponent); return isNegative ? 1 / result : result;}int main() { for (double i = 1.1; i < 1.5; i+=0.1) { for (int j = 1; j < 10; ++j) cout << i << ' ' << j << ' ' << pow(i, j) << endl; } return 0;}
- 《剑指offer》:自己写一个pow函数
- 自己写一个pow()函数
- C语言10的n次方pow函数不好用,自己写一个简单的
- 自己写的一个身份证验证函数
- 完全自己写的一个Dijkstra函数
- 自己写的一个c++ split函数
- 自己写的一个memset函数
- 自己写的一个memcpy函数
- 自己写的一个strcpy函数
- 自己写一个strcmp函数(C++)
- 写一个自己的监听函数
- 自己写的一个功能类似于strtok函数的函数
- 剑指offer—写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。
- 【面试题】剑指offer11--求一个数的指数次方,模拟实现一个pow函数
- 自己写的一个数据库自定义函数实例
- 自己写的一个用函数实现的日历
- 自己写的一个UBB转换的函数
- 自己写的一个固定表头JS函数
- response time(一)
- [USACO]1.2.3 name that number
- 《三国演义》与“项目管理”——从诸葛亮的锦囊妙计再谈团队管理
- response time(二)
- Servlet的请求流程
- 《剑指offer》:自己写一个pow函数
- Navicat 连接mysql失败: Host '*' is not allowed to connect to this MariaDB server
- 过客--期货业协会这个傻逼网站报个名一直报不上提示“基础数据为空”
- KMP算法——字符串匹配
- Windows系统 为 Visual Studio软件 搭建 OpenCV2 开发环境
- C++编程经验分享
- git diff “old mode 100755 new mode 100644”
- 关于“WA”后感
- Jquery插件写按钮状态