Speed Racer must go go go rescue Trixie at the top of Mount Domo! He must get there as quickly as possible, but his Mach 5 only holds a specific amount of fuel, and there is no way to refuel on the way. Luckily, he knows precisely how much fuel is consumed at a particular speed, taking into account air resistance, tire friction, and engine performance. For a given speed v in kilometers per hour, the amount of fuel consumed in liters per hour is
av4 + bv3 + cv2 + dvAssuming Speed travels at a constant speed, his tank holds t liters of fuel, and the top of Mount Domo is m kilometers away, how fast must he drive?
Input
The input will be one problem per line. Each line will contain six nonnegative floating point values representing a, b, c, d, m, and t, respectively. No input value will exceed 1000. The values of c, d, m, and t will be positive. There will always be a solution.
The output should be formatted as a decimal number with exactly two digits after the decimal point, and no leading zeros. The output value should be such that the Speed Racer will not run out of fuel (so truncate, rather than round, the final result). You are guaranteed that no final result will be within 10−6 of an integer multiple of 0.01.
Output
The required output will be a single floating point value representing the maximum speed in kilometers per hour that Speed Racer can travel to reach the top of Mount Domo without running out of gas.
0.000001 0.0001 0.029 0.2 12 100
2.8e-8 7.6e-6 0.0013 0.47 11.65 20.81
1.559e-7 1.8195e-5 0.0022233 0.31292 58.902 85.585
134.41
257.45
142.65
思路:把题目中的式子简化一下就相当于求am*v^3+bm*v^2+cm*v+dm-t=0;求出v的最大值
这题我写的挫了,亮神讲只要把精度调高一点都可以把三分思想转二分法来写
采用二分法求区间(a,b)内方程的根,过程如下:
(1) 取当前可能存在解的区间(a,b);
(2) 若a+0.000001>b或 f((a+b)/2)=0,则可确定根为(a+b)/2并退出过程;
(3) 若f(a)×f((a+b)/2)<0,则根在区间(a, (a+b)/2)中,故对区间(a, (a+b)/2)重复该过程;
(4) 若f(a)× f((a+b)/2)>0,则必然有 f((a+b)/2)×f(b)<0,也就是说根在((a+b)/2,b)中,应该对此区间重复该过程。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <algorithm>
#define INF 0x7fffffff
#define eps 1e-6
using namespace std;
#define mem(a, b) memset(a, b, sizeof(b))
double a,b,c,d,m,sum;
double ff(double x)
{
double tmp=m*a*x*x*x+m*b*x*x+m*c*x+m*d-sum;
return tmp;
}
int main()
{
while(scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&m,&sum) != EOF)
{
double i=0,s,t, mmax=0;
for(i=0; i<=1000; i+=0.01)
{
s=i;
t=i+0.999999;
double mid=(s+t)/2;
if(fabs(ff(s)) > eps && ff(s)*ff(t)<=0)
{
if(fabs(ff(s)) > eps)
mmax=max(mmax,s);
else
{
while(s+eps<t && fabs(ff(mid))>=eps)
{
if(ff(s)*ff(mid)<0)
t=mid;
else
s=mid;
}
mmax=max(mmax,mid);
}
}
}
printf("%.2lf\n",mmax);
}
return 0;
}