求最近的两点坐标

来源:互联网 发布:苹果软件推广 编辑:程序博客网 时间:2024/04/29 21:40
package Java;import java.util.Scanner;public class Domo {public static void main(String args[]){Scanner input = new Scanner(System.in);//输入一共多少个点int number = input.nextInt();double[][] a = new double[number][2];System.out.println("Enter " + number + " Points :\n");for(int i = 0;i < a.length;i ++){a[i][0] = (double)(Math.random() * 10);a[i][1] = (double)(Math.random() * 10);System.out.println("a[" + i + "][0] = " + a[i][0] + "\ta[" + i + "][1] = " + a[i][1]);}//求出0和1两点间距离int p1 = 0,p2 = 1;double shortDistance = Distance(a[0][0],a[0][1],a[1][0],a[1][1]);for(int i = 0;i < a.length;i ++)for(int j = i + 1;j < a.length;j ++){double distance = Distance(a[i][0],a[i][1],a[j][0],a[j][1]);if(distance < shortDistance){p1 = i;p2 = j;shortDistance = distance;}}System.out.println("The colsest two points are " + "(" + a[p1][0] + "," + a[p1][1] + ") and (" + a[p2][0] + "," + a[p2][1] + ")");}//两点间距离public static double Distance(double x1,double y1,double x2,double y2){return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));}}

0 0
原创粉丝点击