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;
}
Distance的更多相关文章
- [LeetCode] Total Hamming Distance 全部汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] Hamming Distance 汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [LeetCode] Shortest Word Distance 最短单词距离
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- C#实现Levenshtein distance最小编辑距离算法
Levenshtein distance,中文名为最小编辑距离,其目的是找出两个字符串之间需要改动多少个字符后变成一致.该算法使用了动态规划的算法策略,该问题具备最优子结构,最小编辑距离包含子最小编辑 ...
随机推荐
- 【blog】MarkDown语法解析为HTML工具
txtmark <dependency> <groupId>es.nitaur.markdown</groupId> <artifactId>txtma ...
- AOP 横行切面编程和 纵向编程 介绍
1 aop:面向切面(方面)编程,扩展功能不修改源代码实现 2 AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码3 aop底层使用动态代理实现(1)第一种情况,有接口情况,使用动态代理创建接口 ...
- 资源中心的ES 服务的COM.IFLYTEK.ERSP.API.RESOURCEAPI 接口注册ZOOKEEPER失败,解决记录
No provider available for the service com.iflytek.ersp.api.ResourceApi from the url zookeeper://zook ...
- package-lock.json和package.json区别
package.json里面定义的是版本范围(比如^1.0.0),具体跑npm install的时候安的什么版本,要解析后才能决定,这里面定义的依赖关系树,可以称之为逻辑树(logical tree) ...
- 20165234 《Java程序设计》第十周课下作业
相关知识点的总结 泛型 Java 泛型的主要目的是可以建立具有类型安全的集合框架,如链表.散列映射等数据结构. 可以使用“class 名称<泛型列表>”声明一个类,为了和普通的类有所区别, ...
- 20165337学习基础和C语言基础调查
20165337学习基础和C语言基础调查 你有什么技能比大多人(超过90%以上)更好? 讲道理我感觉我自己没有什么能比90%以上的人都做得好的技能,我就瘸子里面拔将军挑一个我自认为还不错的技能吧. 我 ...
- oracle 索引的创建与使用
什么是数据库索引 在关系数据库中,索引是一种与表有关的数据库结构,它可以使对应于表的SQL语句执行得更快. 索引的作用相当于图书的目录,可以根据目录中的页码快速找到所需的内容. 对于数据库来说,索引是 ...
- 【转】python编写规范——中标软件有限公司测试中心
[转]python编写规范 一.说明 二.内容 1. 代码布局 1.1 缩进 1.2 表达式和语句中的空格 1.3 行的最大长度 1.4 空行... 1.5 编码... 2. 语句... 2.1 标准 ...
- Java的static类
首先Java的static类只能是静态内部类.如果在外部类声明为static,程序会编译通不过. 其次,主要了解下static内部类与普通内部类的区别是什么,以及static内部类的作用是什么,详见下 ...
- Linux 文件系统IO性能优化【转】
转自:https://blog.csdn.net/doitsjz/article/details/50837569 对于LINUX SA来说,服务器性能是需要我们特别关注的,包括CPU.IO.内存等等 ...