c 实现int sqrt(int x)函数的细节讨论
来源:互联网 发布:oracle和mysql语法区别 编辑:程序博客网 时间:2024/06/07 02:39
题目描述
分析
注意该函数返回的数据类型,是int。那么我们应用二分查找即可实现。细节讨论
于是我便写了如下所示的代码,然而运行超时了。
int mySqrt(int x) { int low=0; int high=x; int mid,product; while(low<=high){ mid=(low+high)/2; product=mid*mid; if(product==x) break; else if(product<x) low=mid+1; else high=mid-1; } if(mid*mid>x) return mid-1; else return mid;}
来看超时的案例说明,
那么问题来了,这么简单的一个代码怎么还会超时呢?代码逻辑没有错呀?
最终发现造成超时的真正原因是值溢出。
来看下图的测试案例,
对于数23456789,将它循环除2的结果是11728394、5864197、…,怎么会有8399098如此大的运行结果呢?原因在这句代码,product=mid*mid; 两个大数相乘值溢出了,每次都溢出,直到不满足low<=high而输出结果。
所以,我们应该谨慎使用乘法,留意是否会有溢出情况发生。若有的话,可以把乘法换成除法防止溢出。
将乘法替换为除法后的代码如下,
int mySqrt(int x) { int low=1; int high=x; int mid; if(x==0) return 0; while(low<=high){ mid=low+(high-low)/2; if(mid==x/mid) break; else if(mid<x/mid) low=mid+1; else high=mid-1; } if(mid>x/mid) return mid-1; else return mid;}
基于同样‘防止溢出’的考虑,把mid=(low+high)/2;改为mid=low+(high-low)/2;会更安全。
小问题,大警醒,所以写下此文。
0 0
- c 实现int sqrt(int x)函数的细节讨论
- c实现double myPow(double x, int n)的细节讨论
- int sqrt(int x)
- Implement int sqrt(int x).
- 【leetcode】sqrt(int x)
- java__X的平方根。设计函数int sqrt(int x),计算 xx 的平方根。
- Sqrt(int x) &&Sqrt(double x)
- LeetCode-- Implement int sqrt(int x)
- 【LeetCode】69、int sqrt(int x).
- leetCode---Implementint sqrt(int x).
- Sqrt(int x) leetcode java
- 【c语言】 编写递归函数,函数应该和下面的函数原型匹配:int hermite(int n,int x))
- LeeCode-Sqrt(x)Implement int sqrt(int x). Compute and return the square root of x.
- 关于C语言交换两个int类型变量的讨论
- [leetcode] sqrt(int num)
- int和unsigned int的细节问题(重要细节)
- 滑动中scrollTo(int x,int y);和scrollBy(int x,int y)的区别(经典)
- String to Int 的细节
- 公钥私钥
- 预处理指令
- Spring MVC 与 CORS
- <优化策略-2>深度学习加速器Layer Normalization-LN
- Redis学习之关闭持久化
- c 实现int sqrt(int x)函数的细节讨论
- ISO-IEC 14443 Type A&B summary
- linux操作系统下安装mysql环境
- Java SSM 商户管理系统 客户管理 库存管理 销售报表 项目源码
- Jfinal 事物应用回滚
- iOS10适配问题,很全的 ^_^
- C与C++的区别。持续更新中……
- C/C++之回调函数
- centos下安装mysql5.7