华中农业大学校赛 I Catching Dogs

本文解析了一道关于追赶移动目标的算法题——抓狗问题。题目要求计算主人公追上按特定速度移动的多只狗所需的时间。文章通过具体样例输入输出展示了问题的解决过程,并提供了完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Catching Dogs

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 1140  Solved: 298
[Submit][Status][Web Board]

Description

 

   Diao Yang keeps many dogs. But today his dogs all run away. Diao Yang has to catch them. To simplify the problem, we assume Diao Yang and all the dogs are on a number axis. The dogs are numbered from 1 to n. At first Diao Yang is on the original point and his speed is v. The ith dog is on the point ai and its speed is vi . Diao Yang will catch the dog by the order of their numbers. Which means only if Diao Yang has caught the ith dog, he can start to catch the (i+1)th dog, and immediately. Note that When Diao Yang catching a dog, he will run toward the dog and he will never stop or change his direction until he has caught the dog. Now Diao Yang wants to know how long it takes for him to catch all the dogs.

Input

 

    There are multiple test cases. In each test case, the first line contains two positive integers n(n≤10) andv(1≤v≤10). Then n lines followed, each line contains two integers ai(|ai|≤50) and vi(|vi|≤5). vi<0 means the dog runs toward left and vi>0 means the dog runs toward right. The input will end by EOF.

Output

    For each test case, output the answer. The answer should be rounded to 2 digits after decimal point. If Diao Yang cannot catch all the dogs, output “Bad Dog”(without quotes).

Sample Input

2 5
-2 -3
2 3
1 6
2 -2
1 1
0 -1
1 1
-1 -1

Sample Output

6.00
0.25
0.00
Bad Dog

HINT

 

 

#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
struct Node
{
    double pos;
    double v;
}dog[15];
double cal(double v,double dogv)
{
    if(v>0&&dogv>0)
    {
        if(v>dogv) return v-dogv;
        else return 0;
    }
    if(v<0&&dogv<0)
    {
        if(v<dogv) return dogv-v;
        else return 0;
    }
    if(v*dogv<=0) return fabs(v+0.0)+fabs(dogv);
}
int main()
{
    int n;
    double v;
    while(scanf("%d%lf",&n,&v)!=EOF)
    {
        if(n<=0) continue;
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf",&dog[i].pos,&dog[i].v);
        }
        double nt=0;
        double ans=0;
        double posp=0;
        bool flag=true;
        for(int i=0;i<n;i++)
        {

            dog[i].pos=dog[i].pos+(dog[i].v*ans);
            if(fabs(posp-dog[i].pos)<1e-6) continue;
            if(posp>dog[i].pos) v=-fabs(v);
            if(posp<dog[i].pos) v=fabs(v);
            double catv=cal(v,dog[i].v);
            if(catv==0) {printf("Bad Dog\n");flag=false;break;}
            double catx=posp-dog[i].pos;
           // cout<<catx<<"**"<<catv<<endl;
            nt=catx/catv;
            posp=fabs(nt)*v+posp;
           // cout<<"posp**"<<posp<<endl;
            ans+=fabs(nt);
        }
        if(flag) printf("%.2lf\n",ans);
    }
    return 0;

}
View Code

模拟题,水题。

考虑dogv=0的这种情况。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值