TJOJ 1084: 最近的距离

题目描述

    在新格尔王国神奇的土地上,有着一群神奇的人们。有一天,三个很无聊的人,菜哭文,牛哭文,牛乐武聚在了一起,他们实在是太无聊了,就开始了一个游戏。菜哭文和牛哭文分别从不同的地方,向着某个方向,同时跑起来。让牛乐武来计算,如果他们一直跑下去,两个人跑动中最近的距离是多少。

         牛乐武当然是能算出来了,但是他觉得这件事情很无聊,他并不想做,所以他把问题丢给了你。

请注意:在计算距离的时候,可以认为两个人没有体积,如果两个人相撞了,那他们的距离为零。

输入

一行,8个实数,菜哭文的初始坐x1,y1,菜哭文的速度向量dx1,dy1;牛哭文的初始坐标x2,y2,牛哭文的速度向量dx2,dy2

输出

一行,两人的最近距离,保留9位小数。

样例输入

-1.0 0.0 1.0 1.0 1.0 0.0 -1.0 1.0

样例输出

0.000000000

提示


 

样例示意图

速度向量(dx,dy)的含义就是 当前在 (x,y) 位置, 一秒钟以后 就会在(x+dx,y+dy) 位置。在一秒钟走过的路径是两点间的线段。


简单数学问题:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<string>
#include<cstring>
#include<cmath>
using namespace std;
#define ESP 1e-6
 
int main()
{
     double x1,y1,dx1,dy1,x2,y2,dx2,dy2;
     scanf ( "%lf %lf %lf %lf %lf %lf %lf %lf" ,&x1,&y1,&dx1,&dy1,&x2,&y2,&dx2,&dy2);
     
     double a,b,c;
     a =  (dx1-dx2)*(dx1-dx2) + (dy1-dy2)*(dy1-dy2);
     b =  2*(x1-x2)*(dx1-dx2) + 2*(y1-y2)*(dy1-dy2);
     c =  (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
     
//  printf("%lf %lf %lf\n",a,b,c);
     
     if ( fabs (a)<ESP)
     {
         if (b<0)
         {
             printf ( "0.000000000\n" );
         }
         else
         {
             if ( fabs (c)<ESP){
                 printf ( "0.000000000\n" );
             } else {
                 printf ( "%.9lf\n" , sqrt ( fabs (c)));   
             }
             
         }
     }
     else
     {
         double minx = -0.5*b/a;
         if (minx<0)
         {
             if ( fabs (c)<ESP){
                 printf ( "0.000000000\n" );
             } else {
                 printf ( "%.9lf\n" , sqrt ( fabs (c)));   
             }
             
         }
         else
         {
             double ans = a*minx*minx + b*minx + c;
             if ( fabs (ans)<ESP){
                 printf ( "0.000000000\n" );
             } else {
                 printf ( "%.9lf\n" , sqrt ( fabs (ans))); 
             }
         }      
     }      
     
     return 0;
}






猜你喜欢

转载自blog.csdn.net/S031302306/article/details/79442219
今日推荐