题解 | #求解立方根#

求解立方根

http://www.nowcoder.com/practice/caf35ae421194a1090c22fe223357dca

二分查找法

#include <algorithm>
#include <cmath>
#include <iomanip>

using namespace std;

int main() {
    double x;
    while(cin>>x){
        double out;
       double left,right;
        if(x>=0&&x<1) {
            left = x;
            right = 1.0;
        }
        else if(x<0&&(x>-1)){
            right = x;
            left = -1.0;
        }
        else if (x>=1){
            left = 1.0;
            right = x;
        }
        else if (x<=1){
            left = x;
            right = -1.0;
        }
        while((right-left)>0.01){
            out = (right+left)/2;
            if(out*out*out>x) right =out;
            else left = out;
            
        }
        
        cout<<fixed<<setprecision(1)<<out<<endl;
    }
}
全部评论

相关推荐

牛客92804383...:在他心里你已经是他的员工了
点赞 评论 收藏
分享
06-10 21:15
门头沟学院 Java
宁阿:好多这种没🧠的公司,他们估计都不知道毕业的人不能给安排实习岗
实习吐槽大会
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务