您的位置:首页 > 其它

[LeetCode]Sqrt(x)

2013-11-18 16:09 417 查看
Implement
int sqrt(int x)
.

Compute and return the square root of x.

思考:参考链接:/article/5111939.html

class Solution {
public:
int sqrt(int x) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
double ans = x;
while(abs(ans * ans - x) > 0.0001)
{
ans = (ans + x / ans) / 2;
}
return (int)ans;
}
};


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: