hzauoj Problem I: Catching Dogs (数学模拟)

来源:互联网 发布:约瑟夫问题 c语言 编辑:程序博客网 时间:2024/06/07 03:06

Problem I: 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 1ton. At first Diao Yang is on the original point and his speed isv. Theith dog is on the point ai and its speed isvi. 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)thdog, 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 integersn(n≤10) andv(1≤v≤10). Thenn lines followed, each line contains two integersai(|ai|≤50) andvi(|vi|≤5). vi<0 means the dog runs toward left and vi>0means 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 -32 31 62 -21 10 -11 1-1 -1

Sample Output

6.000.250.00Bad Dog
//:题意:输入n,v,表示有n条狗,小明的速度是v,接下来输入n对数,xi,vi,表示相应的狗的初始位置和速度(vi小于0的话表示狗向左跑,大于0表示向右跑),
小明有n条狗,现在他们都在数轴上以相应速度的相应的方向跑,现在小明想捉住他们,问需要时间是多少?
//思路:
一个一个模拟就行了,没啥说的
#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>#include<iostream>#define INF 0x3f3f3f3f#define E 1e-6using namespace std;int main(){int n;double v;while(scanf("%d%lf",&n,&v)!=EOF){double t=0,tt;double x[110],y[110],xx=0; for(int i=0;i<n;i++){scanf("%lf%lf",&x[i],&y[i]);}int flag=0;for(int i=0;i<n;i++){x[i]+=t*y[i];if(x[i]==xx)continue;else{if(y[i]<0){if(xx-x[i]>E){if(fabs(y[i])>=v){flag=1;break;}else{tt=fabs(xx-x[i])/fabs(v+y[i]);t+=tt;xx-=tt*v;}}else{tt=fabs(xx-x[i])/fabs(v-y[i]);t+=tt;xx+=tt*v;}}else{if(xx-x[i]>E){tt=fabs(xx-x[i])/fabs(v+y[i]);t+=tt;xx-=tt*v;}else{if(y[i]>=v){flag=1;break;}else{tt=fabs(xx-x[i])/fabs(v-y[i]);t+=tt;xx+=tt*v;}}}}}if(flag)printf("Bad Dog\n");elseprintf("%.2lf\n",t);}return 0;}


0 0
原创粉丝点击