首页 > 试题广场 >

绕距

[编程题]绕距
  • 热度指数: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
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        //第一行输入两个整数x1,y1表示起点横纵坐标,中间用空格隔开
        int x1=sc.nextInt();
        int y1=sc.nextInt();
        //第二行输入两个整数x2,y2表示终点横纵坐标,中间用空格隔开
        int x2=sc.nextInt();
        int y2=sc.nextInt();
        //计算公式,计算结果使用double类型接收
        double dE=Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2));
        double dM=Math.abs(x1-x2)+Math.abs(y1-y2);
        //两点之间绕距
        double D=Math.abs(dM-dE);
        System.out.println(D);

    }
}
发表于 2025-10-30 19:36:48 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int x1 = sc.nextInt();
        int y1 = sc.nextInt();
        sc.nextLine();  // 消耗换行符\n
        int x2 = sc.nextInt();
        int y2 = sc.nextInt();
        sc.nextLine();

        int dx = x1 - x2;
        int dy = y1 - y2;

        double dM = Math.abs(dx) + Math.abs(dy);
        double dE = Math.sqrt(dx * dx + dy * dy);

        double res = Math.abs(dM - dE);

        System.out.println(res);

        sc.close();
    }
}

  • 数据类型:所有距离计算结果用 double 存储(双精度浮点数,有效数字 15-17 位),完全覆盖题目中 “误差不超过 1e-6” 的要求。
  • 避免冗余运算:通过提前计算 dx、dy,减少重复的坐标相减;用 dx*dx 代替 Math.pow,进一步降低浮点误差
  • 输出方式:直接用 System.out.println(delta) 输出 double 类型,Java 默认会打印足够的小数位数(如示例 1 会输出 0.5857864376269049),误差远小于 1e-6。
发表于 2025-10-04 22:22:46 回复(0)
 public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int x1 = in.nextInt();
        int y1 = in.nextInt();
        int x2 = in.nextInt();
        int y2 = in.nextInt();
       
        long manhattan = Math.abs((long)x1 - x2) + Math.abs((long)y1 - y2);
        double dx = (double)x1 - x2;
        double dy = (double)y1 - y2;
        double euclidean = Math.sqrt(dx * dx + dy * dy);
        double delta = Math.abs(manhattan - euclidean);
       
        System.out.printf("%.18f%n", delta);
    }
}这都没有过我也是没话说
发表于 2025-09-13 16:28:19 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String[] a = in.nextLine().split(" ");
            String[] b = in.nextLine().split(" ");
            double s1 = Double.valueOf(a[0]) - Double.valueOf(b[0]);
            double s2 = Double.valueOf(a[1]) - Double.valueOf(b[1]);

            double dE = Math.sqrt(s1*s1 + s2*s2);
            double dM = Math.abs(s1) + Math.abs(s2);
            System.out.println(Math.abs(dM - dE));
        }
    }
}

发表于 2025-08-29 14:43: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)