您的位置:首页 > 其它

[LeetCode] Sqrt(x)

2014-07-21 21:47 387 查看
Implement
int sqrt(int x)
.

Compute and return the square root of x.

百度百科里,牛顿迭代解释:http://baike.baidu.com/view/643093.htm?fr=aladdin

已经证明,如果是连续的,并且待求的零点是孤立的,那么在零点周围存在一个区域,只要初始值位于这个邻近区域内,那么牛顿法必定收敛。
详细见此博客:http://blog.csdn.net/wumuzi520/article/details/7026808

class Solution {
public:
int sqrt(int x) {
double ans = x;
while(abs(ans*ans-x)>0.0001)
{
ans = (ans+x/ans)/2;
}
return ans;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: