首页 > 代码库 > HW7.8
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。