题解 | #求解立方根#

求解立方根

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;
    }
}
全部评论

相关推荐

仁者伍敌:牛子这些人还会点一个自动回复,boss都不带回复的
点赞 评论 收藏
分享
后来123321:别着急,我学院本大二,投了1100份,两个面试,其中一个还是我去线下招聘会投的简历,有时候这东西也得看运气
无实习如何秋招上岸
点赞 评论 收藏
分享
但听说转正率很低,我现在有在实习了,好纠结要不要去
熬夜脱发码农:转正率低归低,但是实习的经历你可以拿着,又不是说秋招不准备了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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