Given a non-negative integer x
, compute and return the square root of x
.
Since the return type is an integer, the decimal digits are truncated, and only the integer part of the result is returned.
Note: You are not allowed to use any built-in exponent function or operator, such as pow(x, 0.5)
or x ** 0.5
.
//暴力解法
int mySqrt(int x){
long int i;
if(x == 1 || x == 2 || x == 3)
return 1;
for(i=0;i<x;i++)
{
if(x<(i*i))
return i - 1;
}
return 0;
}
//