问题 E: THE TOWER
时间限制: 1 Sec 内存限制: 128 MB Special Judge
提交: 222 解决: 30
[提交] [状态] [命题人:admin]
题目描述
The Tower shows atall tower perched on the top of a rocky mountain. Lightning strikes, setting the building alight, and two people leap frnm the windows, head first and arms outstretched.
It is a scene of chaos and destruction.
There is a cone tower with base center at (0, 0, 0), base radius r and apex (0, 0, h) . At time 0 , a point located at ( x0 ,y0, z0) with velocity (vx,vy,vz). What time will they collide? Here is the cone tower.
输入
The first line contains testcase number T (T≤1000), For each testcase the first line contains spaceseparated real numbers rand h (1≤r,h≤1000) the base radius and the cone height correspondingly.
For each testcase the second line contains three real numbers x0 ,y0, z0 (0≤|x0|,|y0|,z0≤1000). For each testcase the third line contains three real numbers vx,vy,vx (). It is guaranteed that at time 0 the point is outside the cone and they will always collide.
输出
For each testcase print Case i: and then print the answer in one line, with absolute or relative error not exceeding 10-6
样例输入
2 1 2 1 1 1 -1.5 -1.5 -0.5 1 1 1 1 1 -1 -1 -1
样例输出
Case 1: 0.3855293381 Case 2: 0.5857864376
(题意:给出一个底部圆圆心为(0,0,0),半径为r,高为h的圆锥,问起始位置为(x0,y0,z0),方向为(vx,vy,vz)的点撞上圆锥的时间。)
(思路:假设相撞的点为(x,y,z),根据下图的相似三角形,我们可以得出下图中的方程,解方程,加判断即可。)
#include <bits/stdc++.h>
using namespace std;
double r,h,x,y,z,vx,vy,vz;
bool judge(double t)
{
double xx,yy,zz;
xx=x+vx*t;
yy=y+vy*t;
zz=z+vz*t;
if(zz>=0&&zz<=h&&(xx*xx+yy*yy)<=r*r)
{
return 1;
}
return 0;
}
int main(void)
{
int t;
scanf("%d",&t);
for(int cas=1;cas<=t;cas++)
{
scanf("%lf%lf",&r,&h);
scanf("%lf%lf%lf%lf%lf%lf",&x,&y,&z,&vx,&vy,&vz);
double a,b,c;
a=h*h*vx*vx+h*h*vy*vy-r*r*vz*vz;
b=2*h*h*vx*x+2*h*h*vy*y+2*r*r*h*vz-2*r*r*z*vz;
c=h*h*x*x+h*h*y*y-h*h*r*r-r*r*z*z+2*r*r*h*z;
double ht=b*b-4*a*c;
double t1=(sqrt(ht)-b)/(2.0*a);
double t2=-1.0*(sqrt(ht)+b)/(2.0*a);
double t=1e18;
if(judge(t1))
t=min(t,t1);
if(judge(t2))
t=min(t,t2);
printf("Case %d: ",cas);
printf("%.10f\n",t);
}
return 0;
}