Skip to content

Instantly share code, notes, and snippets.

@py4
Created February 13, 2014 22:33
Show Gist options
  • Save py4/8985332 to your computer and use it in GitHub Desktop.
Save py4/8985332 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cmath>
#include <climits>
using namespace std;
double truncate(long double number, int n)
{
return (int)(number*pow(10,n)) / (float)pow(10,n);
}
long double get_consumption(double a, double b, double c, double d, double v)
{
return a*pow(v,3) + b*pow(v,2) + c*v + d;
}
double max_velocity(double a,double b, double c, double d, double start, double end,double max_consumption)
{
/*cout << "start: " << start << endl;
cout << "end: " << end << endl;*/
double temp;
double new_v = (start + end) / 2;
//if((temp = truncate(new_v,3)) == truncate(start,3) or temp == truncate(end,3))
if(abs(new_v - start) < 10e-6 or abs(new_v - end) < 10e-6)
{
// cout << "temp: " << temp << endl;
// cout << "start: " << truncate(start,3) << endl;
// cout << "end: " << truncate(end,3) << endl;
//return truncate(temp,2);
return truncate(new_v,2);
}
else
{
double start_consumption = get_consumption(a,b,c,d,start) - max_consumption;
double new_consumption = get_consumption(a,b,c,d,new_v) - max_consumption;
double end_consumption = get_consumption(a,b,c,d,end) - max_consumption;
if((start_consumption > 0 and new_consumption < 0) or (start_consumption < 0 and new_consumption > 0))
//cout << "fuck:" << endl;
//cout << abs(start_consumption - end_consumption) << endl;
//if(abs(start_consumption - end_consumption) < pow(10,-6))
return max_velocity(a,b,c,d,start,new_v,max_consumption);
else
//if((new_consumption > 0 and end_consumption < 0) or (new_consumption < 0 and end_consumption > 0))
return max_velocity(a,b,c,d,new_v,end,max_consumption);
/*else
return max_velocity(a,b,c,d,start,new_v,max_consumption);*/
}
}
int main()
{
double a,b,c,d,m,t;
while(cin >> a >> b >> c >> d >> m >> t)
cout << max_velocity(a,b,c,d,0,pow(10,6),t/m) << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment