Implement int sqrt(int x)
.
Compute and return the square root of x.
牛顿迭代
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;
double value = x;
double last = value;
double next = value;
do
{
last = next;
next = (last+value/last)/2.0;
}while(abs(next-last)>0.000001);
return next;
}
};