Problem I: Catching Dogs

来源:互联网 发布:软件销售好做吗 编辑:程序博客网 时间:2024/06/05 20:20

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) and v(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<math.h>#include<iostream>#include<stdio.h>#include<algorithm>using namespace std;int main(){    double n,v;    double a[12],v1[12];    while(~scanf("%lf%lf",&n,&v))    {        int i,j;        for(i=0; i<n; i++)        {            cin>>a[i];            cin>>v1[i];        }        int w;        w=0;        double t=0.00,t1;        int flag=0;        for(i=0; i<n; i++)        {            if(v>fabs(v1[i]))            {               if(a[i]*v1[i]<0)               {                   t1=fabs(fabs(a[i])-w)/(v+v1[i]);                   if()                   w+=v1[i]*t1;               }               else if(a[i]*v1[i]>0)               {                   t1=fabs(fabs(a[i])-w)/(fabs(fabs(v1[i])-v));                    w+=v1[i]*t1;               }               flag=1;            }            else if(v==fabs(v1[i])&&a[i]==w)            {                flag=1;                t1=0;            }           // printf("t1==%d\n",t1);            t+=t1;            for(j=i;j<n;j++)            {                a[j]=t1*v1[j]+a[j];            }        }        if(flag==0)            printf("Bad Dog\n");        else            printf("%.2lf\n",t);    }    return 0;}

别人思路清晰的代码

#include<stdio.h>#include<string.h>#include<math.h>int main(){    int n,v,i,j;    int flag=0;    int dogv[14];//狗速度    double pos;//人的位置    double t,t1;//时间。t1代表追上一条狗的时间    double dogp[14];//狗的位置    while(scanf("%d %d",&n,&v)!=EOF)    {        flag=0;        memset(dogp,0,sizeof(dogp));        memset(dogv,0,sizeof(dogv));        pos=0;        t=0;        for(i=1;i<=n;i++)        {            scanf("%lf %d",&dogp[i],&dogv[i]);        }        for(i=1;i<=n;i++)        {            if(pos>dogp[i])//人在右边            {                if(dogv[i]<0)//狗远离人方向                {                    if(fabs(dogv[i])>=v)//相反方向,狗大于人的速度                    {                        printf("Bad Dog\n");                        flag=1;                        break;                    }                    else//人的速度大于狗的速度,计算出时间                    {                        t1=(double)(pos-dogp[i])/(double)(v+dogv[i]);                        pos-=v*t1;                    }                }                else //相向而行                {                    t1=(double)(pos-dogp[i])/(double)(v+dogv[i]);                    pos-=v*t1;                }            }            else  if(pos<dogp[i])//人在左边            {                if(dogv[i]>0)                {                    if(dogv[i]>=v)                    {                        printf("Bad Dog\n");                        flag=1;                        break;                    }                    else                    {                        t1=(double)(dogp[i]-pos)/(double)(v-dogv[i]);                        pos+=v*t1;                    }                }                else                {                    t1=(double)(dogp[i]-pos)/(double)(v-dogv[i]);                    pos+=v*t1;                }            }            else //在同样的位置                t1=0;            t+=t1;            for(j=i;j<=n;j++)//更新其他狗的位置            {                dogp[j]=dogv[j]*t1+dogp[j];            }        }        if(!flag)   printf("%.2lf\n",t);    }    return 0;}
0 0
原创粉丝点击