C++龟兔赛跑

#include <iostream>
using namespace std;

const double INF = 0xfffff;//无穷大

double min(double a, double b)
{
    return a > b ? b : a;
}

class Solution
{
private:
    int L, N, C, T;
    int VR, VT1, VT2;
    int path[102];
    double dp[102];

public:
    bool rabbit()
    {
        while (cin >> L)
        {
            cin >> N >> C >> T >> VR >> VT1 >> VT2;
            for (int i = 1; i <= N; ++i)
            {
                cin >> path[i];
            }
            double Time = (double)L / VR;
            dp[0] = 0; path[0] = 0; path[N + 1] = L;
            for (int i = 1; i <= N + 1; ++i)
            {
                dp[i] = INF;
                double curTime;
                for (int j = 0; j < i; ++j)
                {                    
                    int len = path[i] - path[j];
                    if (C > len)
                    {
                        curTime = (double)(len / VT1);
                    }
                    else
                    {
                        curTime = (double)(len - C) / VT2 + (double)C / VT1;
                    }
                    curTime += dp[j];
                    if (j > 0)
                    {
                        curTime += T;
                    }
                    dp[i] = min(dp[i], curTime);
                }
            }    
            if (Time > dp[N + 1])
            {
                cout << "What a pity rabbit!" << endl;
            }
            else
            {
                cout << "Good job,rabbit!" << endl;
            }
        }
        system("pause");
        return true;
    }
};

int main()
{
    Solution space;
    space.rabbit();
    return 0;
}

 

上一篇:Java中定义常量(Constant) 的几种方法


下一篇:javascript H5摇一摇事件