69 - Sqrt(x)
2016-03-09 11:22
330 查看
Implement
Compute and return the square root of x.
Subscribe to see which companies asked this question
思路分析:
牛顿迭代法求解平方根。
此时,令f(x) = x^2 - a, 此时迭代格式为:
int sqrt(int x).
Compute and return the square root of x.
Subscribe to see which companies asked this question
思路分析:
牛顿迭代法求解平方根。
此时,令f(x) = x^2 - a, 此时迭代格式为:
class Solution { public: int sqrt(int x) { // Start typing your C/C++ solution below // DO NOT write int main() function if (x==0) {return 0;} if (x==1) {return 1;} double x0 = 1; double x1; while (true){ x1 = (x0+ x/x0)/2; if (abs(x1-x0)<1){return x1;} x0=x1; } } };
相关文章推荐
- Android广播入门
- update语句根据条件更新某字段的值
- redis 问题解决(MISCONF Redis is configured to save RDB snapshots)
- 《算法竞赛入门经典》习题及反思 -<2>
- 【OpenStack】OpenStack系列17之OpenStack私有云设计一
- IOS-第三方开源库
- python脚本工具-2 去除扩展名后提取目录下所有文件名并保存
- [置顶] 史上最全设计模式导学目录(完整版)
- Spring事务配置的五种方式
- UITextView 文字多不绘制,-[<UITitleLayer: 0x5584190> display]: Ignoring bogus layer size
- 【安装】nodesj的addon模块配置使用&报错处理
- 为什么Java中的密码优先使用 char[] 而不是String?
- jquery自学(二)----(字典向,可供方法忘记时查询)
- 大话 CGLIB 和 JDK 生成动态代理类的那点事
- iOS 学习资源
- 文章标题
- java
- KETTLE——(例)简单的字段转换
- 游戏同行的博客
- 用PyInstaller把Python代码打包成单个独立的exe可执行文件