<center style="color&#58;rgba&#40;0&#44;0&#44;0&#44;&#46;87&#41;&#59;">

1191: Distance

时间限制: 1 Sec  内存限制: 32 MB
</center>

题目描述

There is a battle field. It is a square with the side length 100 miles, and unfortunately we have two comrades who get hurt still in the battle field. They are in different positions. You have to save them. Now I give you the positions of them, and you should choose a straight way and drive a car to get them. Of course you should cross the battle field, since it is dangerous, you want to leave it as quickly as you can!

输入

There are many test cases. Each test case contains four floating number, indicating the two comrades' positions (x1,y1), (x2,y2).

Proceed to the end of file.

输出

you should output the mileage which you drive in the battle field. The result should be accurate up to 2 decimals.

样例输入

1.0 2.0 3.0 4.0
15.0 23.0 46.5 7.0

样例输出

140.01
67.61

提示

The battle field is a square local at (0,0),(0,100),(100,0),(100,100).

解题思路

首先计算两点所确定的直线l,算出直线方程。计算出直线在x=0上的截距a和在x=100上的截距b,通过比较他们与0和100的关系可确定直线所处的位置,进而可利用勾股定理求出l在正方形内部的长度。
#include <stdio.h>
#include <math.h>
int main()
{
    double x1, x2, y1, y2, x, y, s, k, a, b;
    while (~scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2))
    {
        if (x1 == x2 || y1 == y2)
        {
            printf("100.00\n");
            continue;
        }
        k = (y1 - y2) / (x1 - x2);                          /*算出直线的斜率*/
        a = y1 - k * x1;                                    /*算出直线在直线x=0上的截距a*/
        b = y1 - k * (x1 - 100);                            /*算出直线在直线x=100上的截距b*/
        if (a >= 100)
        {
            if (b < 0)
            {
                x = x1 - (y1 - 100) / k;
                y = (x1 - y1 / k) - x;
                s = sqrt(10000 + y * y);
                printf("%.2f\n", s);
            }
            else if (b < 100)
            {
                x = 100 - (x1 - (y1 - 100) / k);
                y = 100 - b;
                s = sqrt(x * x + y * y);
                printf("%.2f\n", s);
            }
            else printf("0.00\n");
        }
        else if (a >= 0)
        {
            if (b >= 100)
            {
                x = x1 - (y1 - 100) / k;
                y = 100 - a;
                s = sqrt(x * x + y * y);
                printf("%.2f\n", s);
            }
            else if (b >= 0)
            {
                x = 100;
                y = fabs(a - b);
                s = sqrt(x * x + y * y);
                printf("%.2f\n", s);
            }
            else
            {
                x = x1 - y1 / k;
                y = a;
                s = sqrt(x * x + y * y);
                printf("%.2f\n", s);
            }
        }
        else
        {
            if (b >= 100)
            {
                x = x1 - (y1 - 100) / k;
                y = x - (x1 - y1 / k);
                s = sqrt(10000 + y * y);
                printf("%.2f\n", s);
            }
            else if (b > 0)
            {
                x = 100 - (x1 - y1 / k);
                y = b;
                s = sqrt(x * x + y * y);
                printf("%.2f\n", s);
            }
            else printf("0.00\n");
        }
    }
    return 0;
}