首页 > 试题广场 >

绕距

[编程题]绕距
  • 热度指数:37881 时间限制: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
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)
print(abs(dm - de))
发表于 2025-06-29 16:17:06 回复(0)
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
int main() {
    double x1, y1;
    cin >> x1 >> y1;
    double x2, y2;
    cin >> x2 >> y2;
    double de = sqrt(pow((x1 - x2), 2) + pow((y1 - y2), 2));
    double dm = fabs(x1 - x2) + fabs(y1 - y2);
    cout << fixed << setprecision(18) << fabs(dm - de) << endl;
}
发表于 2025-09-08 17:28:25 回复(0)
from decimal import Decimal, getcontext 
getcontext().prec=50
x1,y1=map(int,input().split())
x2,y2=map(int,input().split())
dx=abs(x1-x2)
dy=abs(y1-y2)
dm=Decimal(f"{dx+dy}")
de=Decimal(f"{dx**2+dy**2}")**Decimal('0.5')
dd=f"{abs(dm-de)}"
if '.' in dd:
    l,r=dd.split('.')
    result=f"{l}.{r[:18].ljust(18,'0')}"
else:
    result=f"{dd}.{'0'*18}"
print(result)

发表于 2025-10-22 17:55:19 回复(0)
#include <iostream>
using namespace std;
#include <cmath>
#include <iomanip>


int main(){
    double x1,y1;
    double x2,y2;
    cin >> x1 >> y1 >> x2 >> y2;
    double dE = sqrt(pow(x1-x2,2) + pow(y1-y2,2));
    double dM = std::abs(x1-x2) + std::abs(y1-y2);
    double c = std::abs(dE - dM);
    cout << fixed << setprecision(18);
    cout << c << endl;
    return 0;
}


发表于 2025-12-07 02:44:19 回复(0)
import math
a,b  =  map(int,input().split())
c,d  =  map(int,input().split())
M_distance = abs(c-a)+abs(b-d)
O_distance = math.sqrt(pow(c-a,2)+pow(b-d,2))
print(M_distance-O_distance)
发表于 2025-10-28 18:41:59 回复(0)
/*2025年10月12日00:49:03
输出一个实数,表示两点之间的绕距
Δ
Δ。注意,由于浮点数存在误差,只要您的答案与标准答案之间的误差不超过10−6,您的答案就会被认为是正确的。*/

# include <stdio.h>
# include <math.h>//包含数学函数的头文件

int main()
{
	int x1, y1, x2, y2;
	double dE, dM, data;

	scanf("%d %d\n", &x1, &y1);
	scanf("%d %d", &x2, &y2);

	dE = sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
	//pow() 是 C 标准库 <math.h> 中的一个函数,用于计算一个数的幂。
	// 具体来说,它返回的是第一个参数的第二个参数次幂,即 x^ y。

	dM = (abs(x1 - x2) + abs(y1 - y2));
	//C 库函数 int abs(int x) 返回整数 x 的绝对值。
	//注意:abs() 函数只适用于整数,如果需要计算浮点数的绝对值,需要使用 fabs() 函数。
	
	data = fabs(dM - dE);
	//C 库函数 double fabs(double x) 返回浮点数 x 的绝对值。
	//fabs() 是 C 标准库 <math.h> 中的一个函数,用于计算一个数的绝对值。这个函数在处理数***算时非常有用,可以确保获得数值的非负表示。
	//注意:fabs() 函数可以用于 double、float 和 long double 类型的参数。如果需要计算整数的绝对值,应该使用 abs() 函数。

	printf("%.18f", data);
	return 0;
}

发表于 2025-10-12 01:11:03 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            String city1 = in.nextLine();
            String city2 = in.nextLine();
            String [] ab = city1.split(" ");
            String [] cd = city2.split(" ");

            double x = Math.abs(Integer.valueOf(ab[0])-Integer.valueOf(cd[0]));
            double y = Math.abs(Integer.valueOf(ab[1])-Integer.valueOf(cd[1]));
            double E = Math.sqrt(x*x+y*y);
            double M = x + y ;
            double z = Math.abs(M - E);
            System.out.println(z);
        }
    }

发表于 2025-08-27 18:30:04 回复(0)
import math
t1=input()
t2=input()
x1,y1=t1.split()
x2,y2=t2.split()
de=math.sqrt((int(x1)-int(x2))**2+(int(y1)-int(y2))**2)
dm=abs(int(x1)-int(x2))+abs(int(y1)-int(y2))
t=abs(dm-de)
print(t)

发表于 2025-07-19 16:11:08 回复(0)
#include <stdio.h>
#include <math.h>
int main() {
    int x1, y1, x2, y2;
    scanf("%d %d\n", &x1, &y1);
    scanf("%d %d\n", &x2, &y2);
    int x3 = x1 - x2 < 0 ? -(x1 - x2) : x1 - x2;
    int y3 = y1 - y2 < 0 ? -(y1 - y2) : y1 - y2;
    int M = x3 + y3;
    double E = sqrt((x3 * x3) + (y3 * y3));
    double D = M - E < 0 ? -(M - E) : M-E;
    printf("%.6lf", D);
    return 0;
}
发表于 2025-07-06 15:57:13 回复(1)
import math
a=input()
b=input()
x1,y1=a.split()
x2,y2=b.split()
if float(x1)-float(x2) >=0:
    c=float(x1)-float(x2)
else:
    c=float(x2)-float(x1)

if float(y1)-float(y2) >=0:
    d=float(y1)-float(y2)
else:
    d=float(y2)-float(y1)

if (c+d-math.sqrt(c*c+d*d)) >=0:
    print(c+d-math.sqrt(c*c+d*d))
else :
    print(math.sqrt(c*c+d*d)-c-d)
发表于 2026-01-13 16:30:00 回复(0)
#include <bits/stdc++.h>

using namespace std;

int main() {
    long double x1,x2,y1,y2;
    long double de,dm;
    cin>>x1>>y1>>x2>>y2;
    de = sqrtl(pow(fabsl(x1-x2),2)+pow(fabsl(y1-y2),2));
    dm = fabsl(x1-x2)+fabsl(y1 - y2);
    long double ans = dm - de;
    cout << fixed << setprecision(10) << ans << endl;
}
发表于 2026-01-13 11:36:32 回复(0)
靠,用C/C++的,直接把最后结果输出到小数点后6位就行了

发表于 2026-01-09 15:11:51 回复(0)
#include <stdio.h>
#include <math.h>
int main() 
{
    int x1,y1,x2,y2;
    scanf("%d %d",&x1,&y1);
    scanf("%d %d",&x2,&y2);
    double de=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
    double dm=fabs(x1-x2)+fabs(y1-y2);
    double c=fabs(sqrt(de)-dm);
    printf("%.18lf",c);
}

发表于 2026-01-08 19:39:48 回复(0)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main() {
	int x1, y1, x2, y2;
	if (scanf("%d %d", &x1, &y1) != 2) {
		return 1;
	}
	if (scanf("%d %d", &x2, &y2) != 2) {
		return 1;
	}
	if (!(-1e4 <= x1 && x1 <= 1e4 && -1e4 <= x2 && x2 <= 1e4 && -1e4 <= y1 && y1 <= 1e4 && -1e4 <= y2 && y2 <= 1e4)) {
		return 1;
	}
	printf("%f", fabs(sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) - abs(x1 - x2) - abs(y1 - y2)));
	return 0;
}

发表于 2026-01-06 16:34:44 回复(0)
#自测输入未通过,提交通过
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <string>

const int LIMIT_MAX = 10e4;
const int LIMIT_MIN = -10e4;

void valuidate_coordinate(int x, const std::string & name){
if (x < LIMIT_MIN || x > LIMIT_MAX){
throw std::invalid_argument(name + "超出范围 [" +std::to_string(LIMIT_MIN) + ", " + std::to_string(LIMIT_MAX) + "]");
}
}

double calculate_euclidean_distance(double x1,double y1,double x2,double y2) {
return std::sqrt(std::pow((x1-x2),2)+ std::pow((y1-y2),2));
}

double calculate_manhattan_distance(double x1,double y1,double x2,double y2) {
return std::abs(x1-x2)+std::abs(y1-y2);
}

int main() {
double x1,y1,x2,y2;
std::cin >> x1 >> y1;
std::cin >> x2 >> y2;

try {
valuidate_coordinate(x1,"x1");
valuidate_coordinate(y1,"y1");
valuidate_coordinate(x2,"x2");
valuidate_coordinate(y2,"y2");
}catch (const std::invalid_argument& e){
std::cerr << "输入错误:" << e.what() << "\n";
return -1;
}

std::cout<<std::fixed<<std::setprecision(6) <<std::abs(calculate_manhattan_distance(x1,y1,x2,y2) -calculate_euclidean_distance(x1,y1,x2,y2)) <<std::endl;

return 0;
}
发表于 2025-12-26 11:50:52 回复(0)
import math
x1,y1=map(int,input().split())
x2,y2=map(int,input().split())
de=math.hypot(x1-x2,y1-y2)
dm=abs(x1-x2)+abs(y1-y2)
print(abs(de-dm))
发表于 2025-12-22 22:37:51 回复(0)
#include <iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main(){
    double x1,y1,x2,y2,dE,dM,s;
    cin>>x1>>y1>>x2>>y2;
    double dx=x1-x2,dy=y1-y2;
    dE=sqrt(dx*dx+dy*dy);
    dM=abs(dx)+abs(dy);
    s=abs(dM-dE);
    cout<<fixed<<setprecision(18)<<s;
}
//帮忙看看有什么可以优化的地方吗,谢谢
发表于 2025-12-22 12:29:47 回复(0)
#include <stdio.h>
#include <math.h>

int main() {
    int x1,y1,x2,y2;
    scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
    double a = fabs(x1-x2)+fabs(y1-y2)-sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
    printf("%.10lf",a);
    return 0;
}
发表于 2025-12-18 20:25:54 回复(0)
#自测运行不通过,保存并提交通过
#这次做的时候忘了取绝对值一种是abs()另一种是取相反数
#首先记录第一行和第二行的两个数
x1, y1 = map(int, input().split())
x2, y2 = map(int, input().split())
##先算欧几里得距离
before_sqrt = (x1 - x2) ** 2 + (y1 - y2) ** 2
sqrt = before_sqrt ** 0.5
#再计算曼哈顿距离
#x方向的绝对值
x_dis = x1 - x2
y_dis = y1 -y2
mx = abs(x_dis)
my = abs(y_dis)
dm = mx + my
#绕据
bridge = dm - sqrt
deta = abs(bridge)
print(deta)

发表于 2025-12-18 20:20:41 回复(0)
这题抽什么风了啊,小数点精度要求不是不超过10的-6次方就行吗?我这个怎么要求必须精度完全不差才能过?


发表于 2025-12-18 15:52:57 回复(0)