http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5074
There's a round medal fixed on an ideal smooth table, Fancy is trying to throw some coins and make them slip towards the medal to collide. There's also a round range which shares exact the same center as the round medal, and radius of the medal is strictly less than radius of the round range. Since that the round medal is fixed and the coin is a piece of solid metal, we can assume that energy of the coin will not lose, the coin will collide and then moving as reflect.
Now assume that the center of the round medal and the round range is origin ( Namely (0, 0) ) and the coin's initial position is strictly outside the round range. Given radius of the medal Rm, radius of coin r, radius of the round range R, initial position (x, y) and initial speed vector (vx, vy) of the coin, please calculate the total time that any part of the coin is inside the round range.
Please note that the coin might not even touch the medal or slip through the round range.
思路:题意很容易理解就直接说思路了,我是解方程算的,设过t时间硬币进入R区域,那么有
x'=x+t*vx
y'=y+t*vy
带入到方程: (x')^2+(y')^2<=(R+r)^2,解出t1,t2,只有当t1!=t2且t1,t2均大于0时才表示硬币进入到了R,否则直接输出0即可(无解或只有一解或有解小于0),然后用相同的方法判断硬币是否进入Rm,如果没有碰到,则|t1-t2|就是答案,否则计算出硬币碰到Rm的时间t3,设t1>t2,那么2*(t3-t2)即为答案(因为硬币碰到Rm后做反射运动)。
代码如下:
#include <iostream> #include <string.h> #include <algorithm> #include <stdio.h> #include <cmath> #define maxn 100100 using namespace std; double Rm,R,r,x,y,vx,vy; int main() { // freopen("dd.txt","r",stdin); while(scanf("%lf%lf%lf%lf%lf%lf%lf",&Rm,&R,&r,&x,&y,&vx,&vy)!=EOF) { double a=vx*vx+vy*vy; double b=2*(vx*x+vy*y); double c=x*x+y*y-(R+r)*(R+r); double deta=b*b-4*a*c; if(deta<=0||a==0) { printf("0.000\n"); continue; } double t1=(-b+sqrt(deta))/(2*a),t2=(-b-sqrt(deta))/(2*a); if(t2>=0)//和R有交点 { c=x*x+y*y-(Rm+r)*(Rm+r); double deta1=b*b-4*a*c; double time; if(deta1<=0)//没有碰到medal { time=t1-t2; } else { double t3=(-b-sqrt(deta1))/(2*a); time=2*(t3-t2); } printf("%.3lf\n",time); } else { printf("0.000\n"); } } return 0; }