pow(x, n),有时递归的时候要注意用变量保存中间结果
来源:互联网 发布:js如何创建数组 编辑:程序博客网 时间:2024/05/28 06:05
double pow(double x, int n) {
if(n==0)
return 1;
else if(n==1)
return x;
else if(x==0)
return 0;
if(n<0){
x=1/x;
n=-n;
}
//这样写就超时了,因为执行了两次子函数
if(n%2==0)
return pow(x,n>>1)*pow(x,n>>1);
else
return x*pow(x,n>>1)*pow(x,n>>1);
double half=pow(x,n>>1);
if(n&0x1) / /&优先级低于==,n&0x1==0永远是0
return x*half*half;
else
return half*half;
}
if(n==0)
return 1;
else if(n==1)
return x;
else if(x==0)
return 0;
if(n<0){
x=1/x;
n=-n;
}
//这样写就超时了,因为执行了两次子函数
return pow(x,n>>1)*pow(x,n>>1);
else
return x*pow(x,n>>1)*pow(x,n>>1);
double half=pow(x,n>>1);
if(n&0x1) / /&优先级低于==,n&0x1==0永远是0
return x*half*half;
else
return half*half;
}
0 0
- pow(x, n),有时递归的时候要注意用变量保存中间结果
- Pow(x,n) x的n次方
- pow(x,n)的实现
- Pow(x, n) / Super Pow
- HDU 1331--DP--(递归保存中间结果)
- LeetCode 563 Binary Tree Tilt(递归 + 中间结果保存)
- 使用表变量保存中间结果来简化我们的查询
- LeetCode 50. Pow(x, n) x的n的次方
- 求x的n次幂算法Pow(x, n)
- LeetCode: Pow(x, n)
- LeetCode Pow(x,n)
- [Leetcode] Pow(x, n)
- LeetCode: Pow(x, n)
- Leetcode: Pow(x,n)
- [LeetCode] Pow(x, n)
- [Leetcode] Pow(x,n)
- Pow(x, n)
- 【leetcode】Pow(x, n)
- Codeforces 6B. President's Office
- 矩阵连乘
- 数据结构队列C++实现 链式和循环
- XE7 BCB不带Midas.dll发布的方法
- 修改linux主机名称
- pow(x, n),有时递归的时候要注意用变量保存中间结果
- [ZedBoard移植嵌入式Linux教程(4)]创建设备树(Device-Tree)文件dts
- 数据结构二叉查找树C++实现
- Codeforces 6C. Alice, Bob and Chocolate
- 【LeetCode】Combinations 解题报告
- android SlidingMenu sample的j简单写法
- C++如何合并若干个vector
- C++多态的实现原理
- lua实现sleep