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;}
- Problem I: Catching Dogs
- Problem I: Catching Dogs
- hzauoj Problem I: Catching Dogs (数学模拟)
- Problem I: Catching Dogs 几何水题
- 华中农业大学第四届程序设计大赛网络同步赛Problem I: Catching Dogs
- Problem I: Catching Dogs——华中农业大学第四届程序
- HZAU 1018 Catching Dogs
- HZAU--20--Catching Dogs(模拟)
- Problem I
- Problem I
- Problem I
- Problem I
- Problem I
- Problem I
- Problem-I
- Problem I
- Problem B & Problem I
- Problem : 1022 ( Train Problem I )
- 互联网服务端技术——如何学(下A)
- Sicily|2499. 平方数
- Android研究
- 作业四
- 面试小结
- Problem I: Catching Dogs
- HeadFrist设计模式学习之做一个万能遥控器(命令模式)
- CSS 的优先级机制[总结]
- U盘安装ubuntu kylin【双系统】
- Qt Windows下的exe执行文件中获取exe的版本号遇到的error
- Asp.net 学习记录5
- 毕设之opencv椭圆批量生成与拟合
- Dicom 学习
- Spark系统运行内幕机制循环流程