Chinaunix首页 | 论坛 | 博客
  • 博客访问: 253778
  • 博文数量: 170
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1709
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-06 18:01
文章分类

全部博文(170)

文章存档

2016年(11)

2015年(130)

2014年(29)

分类: Java

2015-08-12 16:27:57

//Implement int sqrt(int x).
//
//Compute and return the square root of x.
public class Sqrt {


public static void main(String[] args) {
// TODO Auto-generated method stub


}
public int mySqrt(int x) {
//        return (int) Math.sqrt(x);
 
//牛顿迭代法
  if (x ==0)  
           return 0;  
       double pre;  
       double cur = 1;  
       do  
       {  
           pre = cur;  
           cur = x / (2 * pre) + pre / 2.0;  
       } while (Math.abs(cur - pre) > 0.00001);  
       return (int) (cur);  
   }
}
阅读(642) | 评论(0) | 转发(0) |
1

上一篇:Text Justification

下一篇:ClimbStairs

给主人留下些什么吧!~~