题目描述
此处只需要注意一点,就是三次方存在复数,对于复数情况下,l = input, r = 0
算法1
(二分) O(logn)
C++ 代码
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main()
{
double input;
cin >> input;
double l, r;
if(input >= 0) l = 0, r = input;
else l = input, r = 0;
while(fabs(l - r) > 1e-8)
{
double mid = (l + r) / 2;
if(mid * mid * mid >= input) r = mid;
else l = mid;
}
printf("%.6f\n", l);
return 0;
}