Implement int sqrt(int x)
.
Compute and return the square root of x.
class Solution {
public:
int sqrt(int x) {
if (x ==0)
return 0;
double tmp;
double res = 1;
do
{
tmp = res;
res = x / (2 * tmp) + tmp / 2.0;
} while (abs(res - tmp) > 0.00001);
#if 1
std::cout << (int) res << std::endl;
#endif // 1
return (int)res;
}
};