一. 用二分法来一波.....
#include <iostream>
using namespace std;
double sqrtCount(double x, double precision) {
if (x < 0)
return -1;
double low = 1, up = x;
if (x > 0 && x < 1)
low = x, up = 1;
while (low <= up) {
double mid = low + (up - low) / 2.0;
if (abs(mid*mid - x) <= precision)
return mid;
else if (mid*mid > x)
up = mid;
else if (mid*mid < x)
low = mid;
}
return -1;
}
int main() {
double x = 11, precision = 0.000001;
double res = sqrtCount(x, precision);
cout << res << endl;
cin.get();
return 0;
}