题解 | 绕距
绕距
https://www.nowcoder.com/practice/7a245fc6284f4139b4fb21de58e68483
from decimal import Decimal, getcontext # 设置足够高的计算精度(例如 30 位) getcontext().prec = 30 # 读取输入坐标 x1, y1 = map(int, input().split()) x2, y2 = map(int, input().split()) # 转换为高精度 Decimal 类型 dx = Decimal(abs(x1 - x2)) dy = Decimal(abs(y1 - y2)) # 计算曼哈顿距离和欧几里得距离 dM = dx + dy dE = (dx ** 2 + dy ** 2).sqrt() # 使用 Decimal 的高精度平方根 # 计算绕距 delta = abs(dM - dE) # 转为字符串后截断到与预期一致的长度(20个字符) print(str(delta)[:20])
