C++:求立方根

#include <iostream>
#include<iomanip>

using namespace std;

double getValue(double a, double b, double target)
{
    double ret = (a + b)/2;
    double val = ret*ret*ret;

    if((val - target) * (val - target) < 0.0001)
        return ret;

    if(val*val > target*target)
        return getValue(a, ret, target);
    else if(val*val < target*target)
        return getValue(ret, b, target);
}

double getCubeRoot(double input)
{
    if(input < 1.0)
        return getValue(input, 1.0, input);
    if(input > 1.0)
        return getValue(1.0, input, input);
}

int main()
{
    double a, b;
    cin >> a;
    b = getCubeRoot(a);

    cout <<  setprecision(2) << b << endl;
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章