【小数二分】Acwing790.数的三次方根
作者:互联网
Acwing790.数的三次方根
题解
做法一:通过二分运算到符合精度要求
#include <iostream>
#include <cstdio>
using namespace std;
#define f 1e-8
int main()
{
double x;
cin >> x;
double bg = -10000, ed = 10000, md, temp;
while(ed-bg > f){
md = (bg+ed)/2;
temp = md*md*md;
if(temp >= x)
ed = md;
else
bg = md;
}
printf("%.6lf\n",md);
return 0;
}
做法二:进行多次循坏,达到精度要求
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
#define f 1e-6
int main()
{
double x;
cin >> x;
double bg = -10000, ed = 10000, md, temp;
for(int i = 0; i < 100; ++i){
md = (bg+ed)/2;
temp = pow(md,3);
if(temp >= x)
ed = md;
else
bg = md;
}
printf("%.6lf\n",md);
return 0;
}
标签:md,bg,10000,temp,ed,方根,include,Acwing790,小数 来源: https://www.cnblogs.com/czy-algorithm/p/16243932.html