赞
踩
类似https://blog.csdn.net/SHAOYEZUIZUISHAUI/article/details/106452586
-
- //注意审题:不用任何数学库函数,所以我们只用加减乘除
- #include <iostream>
- using namespace std;
- //二分搜索求开n次方
- //double epr = 10E-14;
- #define epr 10E-14
- double Mysqrt(double m, int n)
- {
- //double 十进制小数点后十五位精确
- double left = 0;
- double right = m;
- //必须保证第一次while循环可以进去
- double mid = 0;
- double temp=m;
- while (mid - temp>epr ||mid - temp<-epr)
- {
- temp = m;
- mid = left + (right - left) / 2;
-
- //m除以mid n-1次,比如求开方,就除mid一次,再和mid比较
- for (int i = 0; i<n-1; i++)
- temp = temp / mid;
- //二分
- if (mid>temp)
- //因为是保留小数点12位,所以,right=mid而非二分查找的mid+1
- right = mid;
- else
- left = mid;
- }
-
- return temp;
- }
- int main()
- {
- double m;
- int n;
- while (cin >> m >> n)
- {
- //输出13位有效数字,前提是有足够多的有效数字
- //cout.precision(13);
- //cout << Mysqrt(m, n);
- printf("%.12lf\n",Mysqrt(m,n));
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。