HW7.8
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HW7.8相关的知识,希望对你有一定的参考价值。
1 import java.util.ArrayList; 2 import java.util.Scanner; 3 4 public class Solution 5 { 6 public static void main(String[] args) 7 { 8 ArrayList<String> list = new ArrayList<>(); 9 Scanner input = new Scanner(System.in); 10 System.out.print("Enter the count of points: "); 11 int pointCount = input.nextInt(); 12 double[][] points = new double[pointCount][2]; 13 14 System.out.print("Enter the points: "); 15 for(int i = 0; i < pointCount; i++) 16 for(int j = 0; j < 2; j++) 17 points[i][j] = input.nextDouble(); 18 19 input.close(); 20 21 double shortestDistance = distance(points[0][0], points[0][1], points[1][0], points[1][1]); 22 double currentDistance = shortestDistance; 23 24 for(int i = 0; i < points.length; i++) 25 for(int j = i + 1; j < points.length; j++) 26 { 27 currentDistance = distance(points[i][0], points[i][1], points[j][0], points[j][1]); 28 if(currentDistance < shortestDistance) 29 { 30 list.clear(); 31 shortestDistance = currentDistance; 32 list.add("(" + points[i][0] + " " + points[i][1] + ") (" + points[j][0] + " " + points[j][1] + ")"); 33 } 34 else if(currentDistance == shortestDistance) 35 { 36 list.add("(" + points[i][0] + " " + points[i][1] + ") (" + points[j][0] + " " + points[j][1] + ")"); 37 } 38 } 39 40 System.out.println("The shortest distance is " + shortestDistance); 41 for(int i = 0; i < list.size(); i++) 42 System.out.println(list.get(i)); 43 } 44 45 public static double distance(double x1, double y1, double x2, double y2) 46 { 47 double square = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1); 48 return Math.sqrt(square); 49 } 50 }
以上是关于HW7.8的主要内容,如果未能解决你的问题,请参考以下文章