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,中文名为最小编辑距离,其目的是找出两个字符串之间需要改动多少个字符后变成一致.该算法使用了动态规划的算法策略,该问题具备最优子结构,最小编辑距离包含子最小编辑 ...
随机推荐
- Python 爬虫一 简介
什么是爬虫? 爬虫可以做什么? 爬虫的本质 爬虫的基本流程 什么是request&response 爬取到数据该怎么办 什么是爬虫? 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间 ...
- Python 8 - Socket编程进阶
本节内容: 1.Socket语法及相关 2.SocketServer实现多并发 Socket语法及相关 socket 概念 socket本质上就是在2台网络互通的电脑之间架设一个通道,两台 ...
- Python基础5-常用模块
本节大纲 模块介绍 time &datetime模块 random os sys shutil shelve xml处理 yaml处理 configparser hashlib subproc ...
- css - 盒子内外边距
css - 盒子内外边距 元素内边距 内边距是指元素包含的内容离元素边框之间的间距,padding会撑大盒子.在浏览器中显示的元素宽高包含了padding. div{ width:200px; ...
- 20165234 [第二届构建之法论坛] 预培训文档(Java版) 学习总结
[第二届构建之法论坛] 预培训文档(Java版) 学习总结 我通读并学习了此文档,并且动手实践了一遍.以下是我学习过程的记录~ Part1.配置环境 配置JDK 原文中提到了2个容易被混淆的概念 JD ...
- 一次悲催的nginx转发白屏经历
背景 公司还有一个学习平台,由于公网地址问题,所以想用nginx转发一下,首先后端地址访问时没有问题的,一切正常. 用nginx转发后,访问nginx代理地址悲催了,出现了白屏. 排查过程 首先贴出来 ...
- WC2019 20天训练
Day -1 2019.1.2 初步计划: 0x60 图论 std 洛谷提高剩余练习 NOIP2018遗留题解 洛谷省选基础练习 数学: 1.数论 2.组合数学(练习:莫比乌斯反演) 3.概率(练习: ...
- 一言难尽的js变量提升
基础知识 在这个课题开始之前我们先做一些基础知识的讲解 1.在顶级的区域内声明的变量为 window级别的变量. 也就是说var a=100 等价于 window.a=100; 2.局部的重新声明变 ...
- 升级Jenkins版本
找到jenkins.war文件的安装目录 cd /usr/local/tomcat/tomcat1_jenkins/webapps/ 停止jenkins service tomcat_jenkins ...
- DUILIB入门简明教程
电子书下载: DUILIB入门简明教程.chm 文章作者: Alberl 电子书制作: 邓学彬 目录: 2013 duilib入门简明教程 -- 前言(1) 2013 duilib入门简明教程 ...