zzuli训练赛_05_13-D

题意:

在一个100*100的战场中有两个伤员,输入会输入两个伤员的坐标x1,y1,x2,y2。问最快该怎么将伤员救出。不是从原点出发,而是将

两个伤员的坐标连线,求该直线在正方形战场中间的距离。

样例输入

1.0 2.0 3.0 4.0
15.0 23.0 46.5 7.0

样例输出

140.01
67.61

解题思路:
利用两点确定直线,利用直线公式找到跟矩形坐标的焦点。需要排序,因为会有四个焦点。

具体代码:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;

struct node
{
    double x;
    double y;
    bool zhen;      
}dian[4];
int cmp(node p,node q)
{
    if(p.x<q.x)
        return 1;
    else
        return 0;
}
bool panduan(node m)
{
    if(m.x>=0.0&&m.x<=100.0&&m.y>=0.0&&m.y<=100.0)
        return true;
    else
        return false;     
}
int main()
{
    double x1=0.0,x2=0.0,y1=0.0,y2=0.0;
    //double shangx,shangy=100.0,xiax,xiay=0.0,zuox=0.0,zuoy,youx=100.0,youy;
    while(scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2)!=EOF)
    {
        memset(dian,0,sizeof(dian));
        double a=0.0;
        double b=0.0;
        if(x1==x2||y1==y2)//平行或垂直 
        {
            printf("%.2lf
",100.00);          
        }
        else
        {
            double temp1x=0.0,temp1y=0.0,temp2x=0.0,temp2y=0.0;
            a=(y1-y2)/(x1-x2);
            b=y1-a*x1;

            dian[0].y=100.0;
            dian[0].x=(dian[0].y-b)/a;
            
            dian[1].y=0.0;
            dian[1].x=(dian[1].y-b)/a;
            
            dian[2].x=0.0;
            dian[2].y=a*dian[2].x+b;
            
            dian[3].x=100.0;
            dian[3].y=a*dian[3].x+b;
            
            sort(dian,dian+4,cmp); 
            temp1x=dian[1].x,temp1y=dian[1].y;
            temp2x=dian[2].x,temp2y=dian[2].y;
            printf("%.2lf
",sqrt((temp1y-temp2y)*(temp1y-temp2y)+(temp1x-temp2x)*(temp1x-temp2x)));
        }
        
    }
    system("pause");
    return 0;    
}
View Code
 
原文地址:https://www.cnblogs.com/baoluqi/p/3731893.html