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. 【CentOS】MySQL的安装

    版本信息:CentOS 7.2 64位 CentOS(Community Enterprise Operating System,中文意思是:社区企业操作系统)是Linux发行版之一,它是来自于Red ...

  2. python(九)迭代器和生成器

    一.迭代 迭代就是逐个并且单向访问容器 (这里的容器暂时指数据类型,比如list和dict) 中的元素的行为.举个例子:将一个长度为五的数组逐个从头到尾(即单向)打印的方式称之为迭代.如下图. > ...

  3. Django学习手册 - 登录验证码

    生成验证码函数 import random from PIL import Image, ImageDraw, ImageFont, ImageFilter _letter_cases = " ...

  4. SpringCloud知识点20190313

    1.SpringBoot和SpringCloud的关系(面试题) Spring Boot 可以离开 Spring Cloud 单独使用开发项目,但是Spring Cloud离不开SpringBoot, ...

  5. Debian 9 美化界面

    Debian 桌面美化 安装 gnome-tweak-tool aptitude install gnome-tweak-tool 登陆gnome-look下载主题包 gnome-look上有很多主题 ...

  6. addEventListener() 方法,事件监听

    知识点1:addEventListener() 方法,事件监听,可以使用 removeEventListener() 方法来移除事件的监听. 语法 element.addEventListener(e ...

  7. 第三章 Models详解

    摘自:http://www.cnblogs.com/xdotnet/archive/2012/03/07/aspnet_mvc40_validate.html Model的概念 万丈高楼平地起,先理解 ...

  8. 消息队列:JMS之基本概念介绍

    摘要:The Java Message Service (JMS) API is a messaging standard that allows application components bas ...

  9. <杂记>该换个背景图了

    ..当然我刚开始也是懵逼的,我有发现这里可以写css,但是还是缺个图片地址,想了想,这不是还有个相册功能吗. 那应该就是把自己要换的图片上传到相册吧. 右击图片,选择检查元素找到图片的src 如:ht ...

  10. LabVIEW--为设备添加配置文件.ini

    需求:我同一个程序下载到两台机器人上,有些参数是不一样的,比如说服务器的ID或者端口,以及存放文件的位置,如果我每次下载之前改程序的话就非常麻烦了(虽然在程序里面是作为全局变量来存的),不利于后期的更 ...