HDU-3086-Need for Speed【解一元二次方程】

3086-Need for Speed

Problem Description
Jiaoshou is a fan of “Need for Speed”, a famous game which most people have played. Recently, Jiaoshou had cleared “Need for Speed Undercover”. In this game, Jiaoshou acted as a spy. To prove that he is not a spy, Jiaoshou must do some evil things frequently. So lots of police cars often ran after him. In order to escape from the chase, Jiaoshou need to accelerate his Bugatti Veyron.

To simplify this problem, we just assume that only one police car ran after Jiaoshou. And Bugatti Veyron’s speed is vx. The police car’s speed is vy. v is a function of first degree or a constant, as the form of a*m+b. In this form, m means the minute that the car has run.
Now Jiaoshou is S meters ahead. Jiaoshou like to know if he could escape from the chase. He want you to write a program to solve this problem.

Input
The input contains several tests. The first line of the input is a single integer T (T < 1000) which is the number of test cases. Then T test cases follow.
Each test case contains three lines. The first line contains a single real number S (0 < S <= 10^6) , as the problem has described. The second line contains two real numbers ax, bx, indicating Bugatti Veyron’s speed is ax*m+bx. The last line also contains two real numbers ay, by, indicating the police car’s speed is ay*m+by. All numbers are range from -1000 to 1000, except S.

Output
If Jiaoshou can escape from the chase, please output “Good driver,Jiaoshou!”. Otherwise, please output the real number M rounded to three digits after the decimal point , in the form of “Oh,God!Jiaoshou will be catched after M minutes.”

Sample Input
2
100
0 10
0 20
1000
0 1
0 0

Sample Output
Oh,God!Jiaoshou will be catched after 10.000 minutes.
Good driver,Jiaoshou!

题目链接:HDU-3086

题目大意:给出教授的速度和警察的速度,用v = am + b的形式.问警察是否能抓到教授。

题目思路:v = am + b可以想到高中物理的加速度(然而已经忘得差不多了orz)
公式:
1. v = v0 + at
2. x = 1/2at^2 + v0t

注意减去初始距离s

以下是代码:

//
// HDU-3086-Need for Speed.cpp
// HDU
//
// Created by pro on 16/5/20.
// Copyright (c) 2016年 loy. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#include<iomanip>
using namespace std;
#define EPS 1e-13
double t1,t2;
void solve(double a,double b,double c)
{
    /* a * t^2 + b * t + c = 0; */
    if (fabs(a) < EPS)
    {
        if (fabs(b) > 0)  t1 = t2 = -c / b;
    }
    else
    {
        double det = b * b - 4.0 * a * c;
        if (det < 0) return;
        else
        {
            t1 = (-b + sqrt(det)) / (2.0 * a);
            t2 = (-b - sqrt(det)) / (2.0 * a);
        }
    }
}
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        double s;
        cin >> s;
        double ax,ay,bx,by;
        t1 = t2 = 0;
        //因为v = v0 + at,所以ax为加速度,ay为初始速度
        cin >> ax >> bx >> ay >> by;
        //因为x = 1/2at^2 + v0t
        //所以求t是否存在(ax - ay) / 2 * t^2 + (bx - by) * t + s = 0;
        solve(0.5*(ay-ax),by-bx,-s);
        if (t1 > 0) printf("Oh,God!Jiaoshou will be catched after %.3lf minutes.\n",t1);
        else if (t2 > 0) printf("Oh,God!Jiaoshou will be catched after %.3lf minutes.\n",t2);
        else cout << "Good driver,Jiaoshou!\n";
    }
    return 0;
}

你可能感兴趣的:(HDU,3086)