×

算法练习(16):计算两点间距离(1.2.1)

96
算法之路
2017.09.13 22:52* 字数 255

本系列博客习题来自《算法(第四版)》,算是本人的读书笔记,如果有人在读这本书的,欢迎大家多多交流。为了方便讨论,本人新建了一个微信群(算法交流),想要加入的,请添加我的微信号:zhujinhui207407 谢谢。另外,本人的个人博客 http://www.kyson.cn 也在不停的更新中,欢迎一起讨论

算法(第4版)

知识点

  • Java创建对象
  • 生成随机数
  • 计算两点之间的距离

题目

1.2.1编写一个Point2D的用例,从命令行接受一个整数N。在单位正方形内生成N个随机点,然后计算两点之间的最近距离


1.2.1 Write a Point2D client that takes an integer value N from the command line, generates N random points in the unit square, and computes the distance separating the closest pair of points.

import java.util.ArrayList;
public class Point2D {
    
    public double x;
    public double y;
    
    //初始化函数
    public Point2D(double x, double y) {
        this.x = x;
        this.y = y;
    }
    
    //计算两点之间的距离
    public double distTo(Point2D that){
        double xDistance = this.x - that.x;
        double yDistance = this.y - that.y;
        return Math.sqrt(xDistance * xDistance + yDistance * yDistance);
    }

    public static void main(String[] args) {
        int N = 10;

        ArrayList<Point2D> points = new ArrayList<Point2D>();
        //创建N个元素的数组
        for (int i = 0; i < N; i++) {
            Point2D point2d = new Point2D(Math.random(), Math.random());
            points.add(point2d);
        }
        //计算第一个点到第二个点的距离
        double resultDistince = points.get(0).distTo(points.get(1));
        
        for (int i = 0; i < N; i++) {
            Point2D point2d = points.get(i);
            for (int j = i + 1; j < N; j++) {
                
                //计算第i个点和第j个点之间的距离
                double distince = point2d.distTo(points.get(j));
                //如果距离比resultDistince小,就赋值给resultDistince
                if (distince < resultDistince) {
                    resultDistince = distince;
                }
                
            }
            
        }
        
        System.out.println("resultDistince:" + resultDistince);
                
    }

}

代码索引

Point2D.java

视频讲解

算法练习(16):计算两点间距离(1.2.1)

日记本
Web note ad 1