leetcod Pow(x, n)
题目:就是实现一个指数函数。
直接用一个while一直乘以n词肯定是会超时的。
自己写了用递归(而且是很挫的递归),测试了无数次,根据每个case去修改代码。终于可以AC了。不忍直视,自己写了好长,如下:
class Solution {
public:
double pow(double x, int n) {
int flag1 = , flag2 = ;
if (n < )
{
flag1 = ;
if (n > INT_MIN) n = -n;
else
{flag2 = ; n = -(n + );}
}
if (n == || x == )
return ;
if (x == )
return ;
int time = (int) (log(n)/log());
double ans = x;
int cnt = ;
while(time--)
{
cnt <<= ;
ans *= ans;
}
if(!flag2)
{
if (cnt == n)
{
if(flag1)
return /ans;
return ans;
}
else
{
if (flag1)
return /(ans*pow(x, n - cnt));
return ans*pow(x, n - cnt);
}
}
else
{
if (cnt == n)
{
if (flag1)
return /(ans*x);
return ans*x;
}
else
{
if (flag1)
return /(x*ans*pow(x, n-cnt));
return x*ans*pow(x, n - cnt);
}
}
}
};
然后肯定要看看其他大神。用递归的,别人十几行就搞定了。
double pow(double x, int n) {
if (n == ) return 1.0;
double half = pow(x, n/);
if (n% == )
{
return half*half;
}
else if (n>)
{
return half*half*x;
}
else
{
return half/x*half;
}
}
以下有一个没有用递归的。
public double pow(double x, int n) {
if(n==)
return 1.0;
double res = 1.0;
if(n<)
{
if(x>=1.0/Double.MAX_VALUE||x<=1.0/-Double.MAX_VALUE)
x = 1.0/x;
else
return Double.MAX_VALUE;
if(n==Integer.MIN_VALUE)
{
res *= x;
n++;
}
}
n = Math.abs(n);
boolean isNeg = false;
if(n%== && x<)
{
isNeg = true;
}
x = Math.abs(x);
while(n>)
{
if((n&) == )
{
if(res>Double.MAX_VALUE/x)
return Double.MAX_VALUE;
res *= x;
}
x *= x;
n = n>>;
}
return isNeg?-res:res;
}
leetcod Pow(x, n)的更多相关文章
- 【探索】无形验证码 —— PoW 算力验证
先来思考一个问题:如何写一个能消耗对方时间的程序? 消耗时间还不简单,休眠一下就可以了: Sleep(1000) 这确实消耗了时间,但并没有消耗 CPU.如果对方开了变速齿轮,这瞬间就能完成. 不过要 ...
- [LeetCode] Super Pow 超级次方
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large posi ...
- [LeetCode] Pow(x, n) 求x的n次方
Implement pow(x, n). 这道题让我们求x的n次方,如果我们只是简单的用个for循环让x乘以自己n次的话,未免也把LeetCode上的想的太简单了,一句话形容图样图森破啊.OJ因超时无 ...
- Javascript四舍五入(Math.round()与Math.pow())
代码 Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ ...
- Pow(x, n)
Implement pow(x, n). public class Solution { public double pow(double x, int n) { //判断x是不是0 if(Math. ...
- leetcode pow(x,n)实现
题目描述: 自己实现pow(double x, int n)方法 实现思路: 考虑位运算.考虑n的二进制表示形式,以n=51(110011)为例,x^51 = x^1*x^2*x^16*x^32,因此 ...
- C语言pow函数编写
C语言pow函数编写 #include<stdio.h> double chaoba(double f,double q); //声明自定义函数 void main(void) { dou ...
- C语言中关于POW在不同状态下四舍五入的解决方法
这是今天作业中的一个代码: #include <stdio.h>#include<math.h>int main(){ printf("请输入一个整数:") ...
- Pow 算法
#include <iostream> using namespace std; template<class T, class Int> T Pow(T x, Int n) ...
随机推荐
- android4.4组件分析--service组件-bindService源代码分析
6.1.1. bindService 由于有前面分析startService的代码实现过程,则对于bindService的代码分析就不用那么具体介绍,在介绍流程的同一时候更关注一些细节上的部分. ...
- hdu3836联通的强还原性点
Equivalent Sets Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 104857/104857 K (Java/Other ...
- 怎么样Ubuntu正在使用root账号登录
一个. 因为当你需要 root 权限,使用 sudo 我们将能够做到这一点.假设你真的需要在 Ubuntu 启用 root 帐户的话,这是最好的运行下面的操作: 1.再次设置 root 的passwo ...
- Ad Hoc
Ad Hoc源自于拉丁语,意思是"for this"引申为"for this purpose only",即"为某种目的设置的,特别的"意思 ...
- HDOJ 3480 Division
斜率优化DP. ... 对数组排序后.dp[i][j]表示对前j个物品分i段的最少代价,dp[i][j]= min{ dp[i-1][k]+(a[k+1]-a[j])^2 }复杂度m*n^2 ...
- Install shipyard
2台机器,192.168.1.153,192.168.1.154 安装Shipyard 1. 154作为集群主节点,在154机器上执行命令 curl -sSL https://shipyard-pro ...
- 设计模式——依赖倒置原则实例(PHP实现)
<?php /** * 设计模式--依赖倒置原则实例 * Created by DannyWang * jue.wang@yulore.com * 2015-05-05 */ abstract ...
- AngularJS 课程
AngularJS 教程(点我) AngularJS 通过新的属性和表达式扩展了 HTML. AngularJS 能够构建一个单一页面应用程序(SPAs:Single Page Application ...
- Service与Activity与交流AIDL
深圳旅游月.终于回来了,做了很多个月,这些天来的东西会慢慢总结出来的.今天,我们正在谈论的Service小东西:沟通. 固定通信的做法比较,基本上按照写模板可以实现. 1.Service与Activi ...
- 一Flash从入门开发者放弃了成长之路
本文将依照入门.成长.转行三个关键词来讲述作者这些年使用Flash进行项目开发的整个历史过程. 一.入门--開始走上Flash的道路. 和Flash的机缘要从大学时代说起.2005年下半年.学校开设了 ...