Problem I: Catching Dogs
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1130 Solved: 292
[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 ithdog 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
题意:主人抓狗的题目 主人拥有n条狗 主人的速度为v n条狗编号后 有对应的初始位置ai与初始速度vi vi>0代表方向向右
当主人抓到第i只狗后才能抓第i+1只狗 如果能抓到所有的狗输出总时间 否则输出 “Bad Dog”;
题解:模拟 注意精度 double
#include<iostream> #include<cstring> #include<cstdio> #include<cmath> using namespace std; double n,v; struct node { double pos; double v; }N[105]; double init=0; double tim(double target,double vv) { double dis,vvv; if(init>target) { dis=init-target; vvv=v+vv; } else { dis=target-init; vvv=v-vv; } if(dis==0) return 0; if(vvv<=0) return -1; return dis*1.0/vvv*1.0; } int main() { while(scanf("%lf %lf",&n,&v)!=EOF) { memset(N,0,sizeof(N)); int flag=0; for(int i=1;i<=n;i++) scanf("%lf %lf",&N[i].pos,&N[i].v); double t=0,tt=0;; init=0; for(int i=1;i<=n;i++) { t=tim(N[i].pos+tt*N[i].v,N[i].v); if(t<0) { flag=1; break; } if(t>0) tt+=t; init=N[i].pos+N[i].v*tt; } if(flag) cout<<"Bad Dog"<<endl; else printf("%.2f ",tt); } return 0; }