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. 调试 - Visual Studio调试

    Visual Studio - 调试 异常处理机制 windows预定义了一系列的异常错误码,每种程序异常都有一个对应的错误码,windows系统将这些类似键值对关系的数据存储在异常处理表中(称为SE ...

  2. Class的isAssignableFrom方法

    Class类的isAssignableFrom是个不常用的方法,感觉这个方法的名字取得不是很好,所以有必要在此解析一下,以免在看源码时产生歧义,这个方法的签名如下: public native boo ...

  3. python使用pudb调试

    pudb是pdb的升级版本 安装 pip3 install pudb 使用方法 在程序文件的开头导入包 from pudb import set_trace set_trace()#断点位置 运行的时 ...

  4. boost::bind 介绍

    boost::bind 介绍   这篇文章介绍boost::bind()的用法, 文章的主要内容是参考boost的文档. 1. 目的 boost::bind 是std::bindlist 和 std: ...

  5. Alpha 冲刺 (3/10)

    目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:冲刺3 团队部分 后敬甲(组长) 过去两天完成了哪些任务 文字描述 组织第一次团队编程 继续阅读小程序开发文档 接下来的 ...

  6. 用Openssl计算ECDSA签名

    ECDSA的全名是Elliptic Curve DSA,即椭圆曲线DSA.它是Digital Signature Algorithm (DSA)应用了椭圆曲线加密算法的变种.椭圆曲线算法的原理很复杂, ...

  7. 51Nod--1384全排列

    1384 全排列 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出一个字符串S(可能又重复的字符),按照字典序从小到大,输出S包括的字符组成的所有排列.例 ...

  8. 切换目录查询目录 tcp

    服务器 import socket import os import json sk = socket.socket() sk.bind(('127.0.0.1',8080)) sk.listen() ...

  9. 【原创】大数据基础之Flume(2)kudu sink

    kudu中的flume sink代码路径: https://github.com/apache/kudu/tree/master/java/kudu-flume-sink kudu-flume-sin ...

  10. Spring Cloud Sleuth超详细实战

    为什么需要Spring Cloud Sleuth 微服务架构是一个分布式架构,它按业务划分服务单元,一个分布式系统往往有很多个服务单元.由于服务单元数量众多,业务的复杂性,如果出现了错误和异常,很难去 ...