题解 | #求解立方根#
求解立方根
https://www.nowcoder.com/practice/caf35ae421194a1090c22fe223357dca
使用二分法解决求解立方根问题,使用printf控制一位小数点数输出
#include <cmath> #include <iostream> using namespace std; int main() { double val = 0; cin >> val; double min = -20.0; double max = 20.0; double mid = 0; while( true ){ mid = (min+max)/2; if( mid*mid*mid - val > 0.005){ max = mid; }else if(val - mid*mid*mid > 0.005) min = mid; else{ break; } } printf("%.1lf\n", mid); } // 64 位输出请用 printf("%lld")