Implement int sqrt(int x)
.
Compute and return the square root of x.
分析:
牛顿迭代法求平方根。
x = (x + n / x) / 2
x会收敛到sqrt(n)
int sqrt(int x) {
double i,j = 1;
do{
i = j;
j = (i + x/i)/2;
}while(abs(j - i) > 1e-6);
return j;
}