欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

求解立方根

程序员文章站 2024-03-22 23:26:58
...
#include<bits/stdc++.h>
using namespace std;
double Fab(double n)
{
    if(n<0)return -n;
    return n;
}
int main()
{
    double n;
    double start,end,mid;
    while(cin>>n)
    {
        if(n<0)
        {
            n=-n;
            cout<<"-";
        }
        else if(n>0)
        {
        }
        else
        {
            cout<<"0.0"<<endl;
            continue;
        }
        start=0,end=1000;
        mid=(start+end)/2;
        while(Fab(mid*mid*mid-n)>0.0001)
        {
            if(mid*mid*mid>n)
            {
                end=mid;
            }
            else
            {
                start=mid;
            }
            mid=(start+end)/2;
        }
        printf("%.1f\n",mid);
    }
    return 0;
}
/*
#include<bits/stdc++.h>
using namespace std;
double Fab(double n)
{
    if(n<0)return -n;
    return n;
}
int main()
{
    double n;
    double start,end,mid;
    while(cin>>n)
    {
        if(n>0)
        {
          if(n>1)
          {
              start=0,end=1000;
              mid=(start+end)/2;
              while(Fab(mid*mid*mid-n)>0.001)
              {
                  if(mid*mid*mid>n)
                  {
                   end=mid;
                  }
                  else {
                    start=mid;
                  }
                  mid=(start+end)/2;
              }
              printf("%.1f\n",mid);
          }
          else if(n<1)
          {
              start=0,end=1;
              mid=(start+end)/2;
              while(Fab(mid*mid*mid-n)>0.001)
              {
                  if(mid*mid*mid>n)
                  {
                   end=mid;
                  }
                  else {
                    start=mid;
                  }
                  mid=(start+end)/2;
              }
              printf("%.1f\n",mid);
          }
          else {
            cout<<"1.0"<<endl;
          }
        }
        else if(n<0)
        {
           n=-n;
           if(n>1)
          {
              start=0,end=1000;
              mid=(start+end)/2;
              while(Fab(mid*mid*mid-n)>0.001)
              {
                  if(mid*mid*mid>n)
                  {
                   end=mid;
                  }
                  else {
                    start=mid;
                  }
                  mid=(start+end)/2;
              }
              printf("-%.1f\n",mid);
          }
          else if(n<1)
          {
              start=0,end=1;
              mid=(start+end)/2;
              while(Fab(mid*mid*mid-n)>0.001)
              {
                  if(mid*mid*mid>n)
                  {
                   end=mid;
                  }
                  else {
                    start=mid;
                  }
                  mid=(start+end)/2;
              }
              printf("-%.1f\n",mid);
          }
          else {
            cout<<"-1.0"<<endl;
          }
        }
        else
        {
            cout<<"0.0"<<endl;
        }
    }
    return 0;
}
*/

相关标签: 算法