Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.43 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. const double eps = 1e-6;
  4.  
  5. int main(){
  6.     double x = 0;
  7.     scanf("%lf", &x);
  8.  
  9.     double tmp = (x >= 0 ? x : -x);
  10.     double left = 0, right = tmp;
  11.     double mid = 0;
  12.  
  13.     while (right - left > eps){
  14.         mid = (left + right) / 2;
  15.         if (mid * mid * mid > tmp)
  16.             right = mid;
  17.         else
  18.             left = mid;
  19.     }
  20.  
  21.     double cubeRoot = (x >= 0 ? (left + right) / 2 : - (left + right) / 2);
  22.     printf("%lf\n", cubeRoot);
  23.  
  24.     return 0;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement