lintcode-打卡题-2327 · 求直角坐标系内两点间距离(Python 版)

266 阅读1分钟

描述

请从标准输入流(控制台)中获取 4 个正整数 x1y1x2y2 表示两个点的坐标 (x1, y1)(x2, y2), 要求计算出这两个点之间的距离(保留两位小数),并且你需要使用 print 语句输出该距离

**

1≤x1​,y1​,x2​,y2​≤10

样例

评测机会通过执行命令 python main.py 来执行你的代码,并将 x1y1x2y2 作为标准输入从控制台输入,每个参数单独一行。

样例一

当 x1 = 1y1 = 3x2 = 5y2 = 4 时,程序执行打印出的结果为:

4.12

解释:

17 的平方根在计算结果中第三位小数是 3,则保留两位小数后是 4.12

样例二

当 x1 = 4y1 = 3x2 = 7y2 = 5 时,程序执行打印出的结果为:

3.61

解释:

13 的平方根在计算结果中第三位小数是 5,所以需要给第二位小数进一位,则保留两位小数后是 3.61

题解

这题主要是接收输入:python是使用input接收输入,在接收输入的同时转成int类型。最后利用print的格式化数据,保留两位小数。python中用来求平方的函数时pow。

python print也支持参数格式化,与C言的printf似,具体的有%f,%d ,%s,%x,%o,%r,

strHello = "the length of (%s) is %d" %('Hello World',len('Hello World'))
print strHello
#输出果:the length of (Hello World) is 11
import math
x1 = int(input())
y1 = int(input())
x2 = int(input())
y2 = int(input())
# output the answer to the console according to the requirements of the question
print('%.2f'%math.sqrt(pow((x1-x2),2)+pow((y1-y2),2)))