Skip to content

Instantly share code, notes, and snippets.

@vitek999
Created December 28, 2017 17:45
Show Gist options
  • Save vitek999/6768316750e1539e413cca3bdd1fb9f1 to your computer and use it in GitHub Desktop.
Save vitek999/6768316750e1539e413cca3bdd1fb9f1 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <iomanip>
#include <stdio.h>
using namespace std;
int main(){
short a = 0,b = 0,c = 0,d = 0;
long double x;
freopen("cubroot.in", "r", stdin);
freopen("cubroot.out", "w", stdout);
cin>>a>>b>>c>>d;
if (a < 0){
a*=-1;
b*=-1;
c*=-1;
d*=-1;
}
long double l = -1e6;
long double r = 1e6;
for(int i = 0; i < 100; ++i){
long double midl = (l + r)/2;
if((a*midl*midl*midl + b*midl*midl + c*midl + d) < 0){
l = midl;
}else{
r = midl;
}
x = l;
}
// printf("%.6llf\n", x);
cout << x;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment