其他分享
首页 > 其他分享> > UPC Contest 1747 THE TOWER

UPC Contest 1747 THE TOWER

作者:互联网

题目描述

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

思路:

           设一直点A(x,y,z),以速度(vx,vy,vz)向圆锥撞上去。所以设碰撞点为B(x0,y0,z0)

则有关系:

x0=x+vx*t;

y0=y+vy*t;

z0=z+vz*t;

 

sqrt(x0*x0+y0*y0)/r=(h-z0)/h;

所以通过这四个式子可以得到一个一元二次方程,求出对应的两个解,并且判断是否满足要求即可。

 

AC代码

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int t;
double r,h,x,y,z,vx,vy,vz;
int main()
{
    cin>>t;
    for(int i=1;i<=t;i++)
    {
        cin>>r>>h;
        cin>>x>>y>>z;
        cin>>vx>>vy>>vz;
        double a=vx*vx*h*h+h*h*vy*vy-vz*vz*r*r;
        double b=2*h*h*x*vx+2*h*h*y*vy+2*h*r*r*vz-2*r*r*z*vz;
        double c=h*h*x*x+h*h*y*y-r*r*h*h+2*r*r*h*z-r*r*z*z;
        double t1=(-b+sqrt(b*b-4*a*c))*1.0/(2*a);
        double t2=(-b-sqrt(b*b-4*a*c))*1.0/(2*a);
        double t3=9999999999999.999;
        if(t1>=0&&(z+vz*t1)>=0&&(z+vz*t1)<=h)
            t3=min(t3,t1);
        if(t2>=0&&(z+vz*t2)>=0&&(z+vz*t2)<=h)
            t3=min(t3,t2);
        printf("Case %d: %.12f\n",i,t3);
    }
    return 0;
}

 

标签:double,x0,UPC,y0,vx,vy,TOWER,1747,vz
来源: https://blog.csdn.net/ytuyzh/article/details/89791505