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

Distance的更多相关文章

  1. [LeetCode] Total Hamming Distance 全部汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  2. [LeetCode] Hamming Distance 汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  3. [LeetCode] Rearrange String k Distance Apart 按距离为k隔离重排字符串

    Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...

  4. [LeetCode] Shortest Distance from All Buildings 建筑物的最短距离

    You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...

  5. [LeetCode] Shortest Word Distance III 最短单词距离之三

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

  6. [LeetCode] Shortest Word Distance II 最短单词距离之二

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

  7. [LeetCode] Shortest Word Distance 最短单词距离

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  8. [LeetCode] One Edit Distance 一个编辑距离

    Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...

  9. [LeetCode] Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  10. C#实现Levenshtein distance最小编辑距离算法

    Levenshtein distance,中文名为最小编辑距离,其目的是找出两个字符串之间需要改动多少个字符后变成一致.该算法使用了动态规划的算法策略,该问题具备最优子结构,最小编辑距离包含子最小编辑 ...

随机推荐

  1. maven(一) 一 修改仓库存放路径

    一.修改仓库存放路径 maven默认的仓库是在C盘下的,这样当重新装系统的时候,仓库就要重新建了.因此可以修改默认存放的位置. 修改仓库的地址在maven安装包(即apache-maven-bin)下 ...

  2. openstack Q版部署-----Cinder云存储服务(10)

    一.cinder介绍 一般 cinder-api 和 cinder-scheduler 安装在控制节点上, cinder-volume 安装在存储节点上. 二.数据库配置 # 在任意控制节点创建数据库 ...

  3. os及os.path练习题

    查找目录下每个文件的数量(考察获取文件后缀名以及获取当前目录下文件方法) import os #获取目录下的所有文件 list = os.listdir('.') filetype = {} for ...

  4. CentOS 7 安装配置 MySQL

    https://blog.imzhengfei.com/centos-7-an-zhuang-pei-zhi-mysql/ MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前 ...

  5. vmware启动虚拟机发现没权限

    前期安装未参与,但了解大致情况: 物理机上安装CentOS7系统,然后安装VMWare,虚拟了几台CentOS6 遇到的问题:物理机重启后,以root进入系统,但打开VMWare显示是普通用户权限,以 ...

  6. Threading.local

    在多线程环境下,每个线程都有自己的数据.一个线程使用自己的局部变量比使用全局变量好,因为局部变量只有线程自己能看见,不会影响其他线程,而全局变量的修改必须加锁. Threading.local可以创建 ...

  7. 使用sudo而无需输入密码的设置

    在linux上,root用户是老大,什么事都能做.但是,很多时候由于安全等各种原因,我们不希望把root用户开放给大家,但是又希望其他的用户可以有root的权限,所以就有了sudo用户.而执行sudo ...

  8. ubuntu 安装source insight3.5

    版本 ubuntu 16.04 在linux下安装 windows下程序,需要安装wine, wine 是 "“Wine Is Not an Emulator" 缩写. 1) 安装 ...

  9. 阿里云主机Nginx下配置NodeJS、Express和Forever

    https://cnodejs.org/topic/5059ce39fd37ea6b2f07e1a3 AngularJS中文社区即运行在阿里云主机上,本站使用Nginx引擎,为了AngularJS,我 ...

  10. 天宝MB-Two:无法打开web登陆界面

    在浏览器中访问http://192.168.1.100,正常是打开MB-Two芯片的web 登陆界面,但是事与愿违,打开的是帮助界面. 解决办法: 用串口调试助手,波特率默认是115200,连接过去. ...