Distance

1191: Distance

时间限制: 1 Sec  内存限制: 32 MB

题目描述

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;
}

转载于:https://www.cnblogs.com/lzyws739307453/p/8908777.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值