diff --git a/c-cpp/15_bsearch/bsearch_c/sqrt.c b/c-cpp/15_bsearch/bsearch_c/sqrt.c new file mode 100644 index 0000000..7c7c3d4 --- /dev/null +++ b/c-cpp/15_bsearch/bsearch_c/sqrt.c @@ -0,0 +1,53 @@ +/************************************************************************* + > File Name: sqrt.c + > Author: jinshaohui + > Mail: jinshaohui789@163.com + > Time: 18-10-31 + > Desc: + ************************************************************************/ +#include +#include +#include +#include + + +/*求解精度设置*/ +#define E 0.000001 +double mybsearch(double num) +{ + double start = 1.0; + double end = num; + double mid = 0.0; + while(1) + { + mid = (start + end)/2; + if(((mid*mid - num) <= E) && ((mid*mid - num) >= -E)) + { + return mid; + } + + if ((mid*mid - num) > E) + { + end = mid; + } + else + { + start = mid; + } + } + + return 0; +} + + +int main() +{ + double num = 0.0; + + /*这里需要注意:double的输入方式*/ + scanf("%lf",&num); + printf("\r\n num %lf的平方根是%lf",num,mybsearch(num)); + + return 0; +} +