https://oj.leetcode.com/problems/powx-n/ 提交地址 快速幂的使用,可以研究一下

 public class Solution {
public double pow(double x, int n) { if(n==0) return 1.0;
if(x==1) return 1.0;
if(x==0)return 0;
if(x==-1&&n%2==0) return 1; //有几个个数据一致通不过,才加入这么多判断,其实判断n=0 n>0 n<0,
int f=1;
if(n<0){ f=-1; n=-n;}
double res=1.0;
double temp=x; while(n!=0)
{
if((n&1)==1)
{
res*=temp;
}
temp=temp*temp;
n=n>>1;
} if(f==1) return res;
else return 1.0/res; }
}

pow(x,n) leecode的更多相关文章

  1. 【探索】无形验证码 —— PoW 算力验证

    先来思考一个问题:如何写一个能消耗对方时间的程序? 消耗时间还不简单,休眠一下就可以了: Sleep(1000) 这确实消耗了时间,但并没有消耗 CPU.如果对方开了变速齿轮,这瞬间就能完成. 不过要 ...

  2. [LeetCode] Super Pow 超级次方

    Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large posi ...

  3. [LeetCode] Pow(x, n) 求x的n次方

    Implement pow(x, n). 这道题让我们求x的n次方,如果我们只是简单的用个for循环让x乘以自己n次的话,未免也把LeetCode上的想的太简单了,一句话形容图样图森破啊.OJ因超时无 ...

  4. leecode系列--Two Sum

    学习这件事在任何时间都不能停下.准备坚持刷leecode来提高自己,也会把自己的解答过程记录下来,希望能进步. Two Sum Given an array of integers, return i ...

  5. Javascript四舍五入(Math.round()与Math.pow())

    代码 Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ ...

  6. Pow(x, n)

    Implement pow(x, n). public class Solution { public double pow(double x, int n) { //判断x是不是0 if(Math. ...

  7. leetcode pow(x,n)实现

    题目描述: 自己实现pow(double x, int n)方法 实现思路: 考虑位运算.考虑n的二进制表示形式,以n=51(110011)为例,x^51 = x^1*x^2*x^16*x^32,因此 ...

  8. C语言pow函数编写

    C语言pow函数编写 #include<stdio.h> double chaoba(double f,double q); //声明自定义函数 void main(void) { dou ...

  9. C语言中关于POW在不同状态下四舍五入的解决方法

    这是今天作业中的一个代码: #include <stdio.h>#include<math.h>int main(){ printf("请输入一个整数:") ...

随机推荐

  1. C++学习指南

    转载于stackoverflow:http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list 感谢Ge ...

  2. PHP CURL参数详解

    curl用法:cookie及post 一.cookie用法 <?php $cookie_jar = tempnam('./tmp','cookie'); // login $c=curl_ini ...

  3. <s:iterator></s:iterator>循环指定输出,(status的方法使用)

    list集合中的实体的一个属性是另一个实体的集合(如下) public class PetInfo { private int petId; private String private Set< ...

  4. (转) sphinx 高亮显示搜索词

    http://hi.baidu.com/tewuapple/item/7a7bc34adbda24a8df2a9fe5  (转)

  5. 【原创】Linux 内核模块编程

    sudo gedit hello.c #include <linux/module.h> #include <linux/kernel.h> #include <linu ...

  6. javascript face ++

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. Python全栈开发之MySQL(三)视图,存储过程触发器,函数,事务,索引

    一:视图 1:什么是视图? 视图是指存储在数据库中的查询的SQL语句,具有简单.安全.逻辑数据独立性的作用及视点集中简化操作定制数据安全性的优点.视图包含一系列带有名称的列和行数据.但是,视图并不在数 ...

  8. [转]ef获取某个表中的部分字段值

    我有个新闻表 id,title,body,createtime,author,click 使用ef4.1 仅仅读取 id,title,createtime 并显示在页面上. public static ...

  9. 动画讲解 Eclipse 常用快捷键

    Eclipse有强大的编辑功能, 工欲善其事,必先利其器, 掌握Eclipse快捷键,可以大大提高工作效率. 小坦克我花了一整天时间, 精选了一些常用的快捷键操作,并且精心录制了动画, 让你一看就会. ...

  10. iOS -一些常用的方法

    1.获取本地的语言 + (NSString *)getLocalLanguage { NSString *language = [[[NSUserDefaults standardUserDefaul ...