题解 | #求解立方根#
求解立方根
http://www.nowcoder.com/practice/caf35ae421194a1090c22fe223357dca
#include<iostream>
using namespace std;
int main()
{
double d;
while(cin>>d)
{
bool op=false;
if(d<0)
{
op=true;
d=d*(-1.0);
}
int tmp=0;
while(abs(d)<1)
{
d*=1000;
tmp++;
}
double minv=0;
double maxv=d;
double midv=(minv+maxv)/2;
while(abs(midv*midv*midv-d)>0.001)
{
if(midv*midv*midv>d)
{
maxv=midv;
}
else if(midv*midv*midv<d)
{
minv=midv;
}
else
{
break;
}
midv=(maxv+minv)/2;
}
while(tmp)
{
midv/=10;
tmp--;
}
if(op)
{
midv=midv*(-1.0);
}
printf("%.1lf\n",midv);
}
return 0;
}