Speed Racer (一元三次方程的求解)

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 + dv
Assuming 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.


Sample Input
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


Sample Output
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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值