首页 > 试题广场 >

绕距

[编程题]绕距
  • 热度指数:32526 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
\hspace{15pt}在城市道路规划中,绕距是一个很重要的概念,指的是城市中两个重要人员聚集点之间的欧几里得距离(欧氏距离)与曼哈顿距离之差的绝对值。一般而言,绕距越小,则城市交通参与者在这两个地点之间所走的“冤枉路”就越小。

\hspace{15pt}欧几里得距离(Euclidean distance)表示两点间的直线距离;曼哈顿距离(Manhattan distance)表示只沿着横平竖直的城市街道从起点到达终点的最短距离。

\displaystyle d_E = \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}

\displaystyle d_M = |x_1 - x_2| + |y_1 - y_2|

\hspace{15pt}定义两点间的绕距为

\displaystyle \Delta = \bigl|d_M - d_E\bigr|

输入描述:
\hspace{15pt}第一行输入两个整数 x_1,y_1-10^4 \leqq x_1,y_1 \leqq 10^4),分别表示起点的横坐标和纵坐标。

\hspace{15pt}第二行输入两个整数 x_2,y_2-10^4 \leqq x_2,y_2 \leqq 10^4),分别表示终点的横坐标和纵坐标。


输出描述:
\hspace{15pt}输出一个实数,表示两点之间的绕距 \Delta。注意,由于浮点数存在误差,只要您的答案与标准答案之间的误差不超过 10^{-6},您的答案就会被认为是正确的。
示例1

输入

0 0
1 1

输出

0.585786437626904951

说明

两点间曼哈顿距离为 2,欧几里得距离为 \sqrt{2},结果为 2- \sqrt{2},约为 0.585786437626904951
头像 牛客875777337号
发表于 2025-07-08 21:46:23
import math # 公式中留参参数 x1,y1 = map(int,input().split()) x2,y2 = map(int,input().split()) # 求公差 gx = x1 - x2 gy = y1 - y2 # 根据公式带入 dm = abs(gx) + ab 展开全文
头像 stcurry
发表于 2025-10-30 20:21:09
#include <stdio.h> #include <math.h> int main() { int x1,x2,y1,y2; double de,dm; scanf("%d %d",&x1,&y1); 展开全文
头像 qwertre
发表于 2025-10-17 22:01:52
a,b=map(int,input().split()) c,d=map(int,input().split()) E=((a-c)**2+(b-d)**2)**0.5 M=((a-c)**2)**0.5+((b-d)**2)**0.5 Z=((E-M)**2)**0.5 print(Z)
头像 午夜人屠
发表于 2025-08-05 18:14:22
#include <stdio.h> #include<math.h> #include <stdlib.h> int main() { int x1,y1 = 0; int x2,y2 = 0; double de = 0; do 展开全文
头像 250531110_李心宁
发表于 2025-09-28 20:03:55
#include <cmath> #include <iomanip> #include <iostream> using namespace std; int main(){ double xx1,xx2,yy1,yy2; double de, 展开全文
头像 CARLJOSEPHLEE
发表于 2025-07-15 20:36:59
想了半天一句话怎么解决,AI想出解包+匿名表达式,学到了 print((lambda x1,y1,x2,y2: abs(x1-x2)+abs(y1-y2) - ((x1-x2)**2 + (y1-y2)**2)**0.5)(*map(int, input().split()), *map(int, 展开全文
头像 牛客758197119号
发表于 2025-11-23 17:22:42
x1,y1 = map(int, input().split()) x2,y2 = map(int, input().split()) de = ((x1-x2)**2 + (y1-y2)**2)**0.5 dm = abs(x1-x2) + abs(y1-y2) m = abs(de-dm) pr 展开全文
头像 派大星zbc
发表于 2025-10-03 21:58:33
#include<bits/stdc++.h> using namespace std; int main(){ double x1,y1,x2,y2,de,dm,n; cin>>x1>>y1>>x2>>y2; de 展开全文
头像 165男生的逆袭
发表于 2025-10-11 09:26:03
#include <stdio.h> #include<math.h> int main() { int x1, y1, x2, y2; //输入数据 scanf("%d %d", &x1, &y1); scanf("% 展开全文
头像 初见月_
发表于 2025-07-31 07:15:52
#include <stdio.h> #include<math.h> #define De(x1,x2,y1,y2) sqrt(pow(x1-x2,2)+pow(y1-y2,2)) #define Dm(x1,x2,y1,y2) abs(x1-x2)+abs(y1-y2) 展开全文