要是不告诉哪里错了,估计尴尬了。。 没考虑负数和小数。
#include using namespace std; int main() { double a; cin>>a; double l=0;
double temp;
bool flag=false;bool flag2=false;
if(a<0)
{
a=-1*a;
flag=true;
}
if(a>0&&a<1)
{
a= 1/a;
flag2=true;
}
double r = a;
while(l<r&&(r-l)>0.0001)
{
temp = (l+r)/2;
if(temp*temp* temp>a)
{
r=temp;
}
else
{
l=temp;
}
}
if(flag&&flag2)
printf("%.1f",-1*1/temp);
if(flag&&!flag2)
printf("%.1f",-1*temp);
if(!flag&&flag2)
printf("%.1f",1/temp);
if(!flag&&!flag2)
printf("%.1f",temp);
return 0;
}