1 public class Solution 2 { 3 public static void main(String[] args) 4 { 5 double[][] points = 6 { 7 {-1, 0, 3}, {-1, -1, -1}, 8 {4, 1, 1}, {2, 0.5, 9}, 9 {3.5, 2, -1}, {3, 1.5, 3}, 10 {-1.5, 4, 2}, {5.5, 4, -0.5} 11 }; 12 13 double shortestDistance = distance(points[0][0], points[0][1], points[0][2], 14 points[1][0], points[1][1], points[1][2]); 15 double currentDistance = shortestDistance; 16 17 for(int i = 0; i < points.length; i++) 18 { 19 for(int j = i + 1; j < points.length; j++) 20 { 21 currentDistance = distance(points[i][0], points[i][1], points[i][2], 22 points[j][0], points[j][1], points[j][2]); 23 if(currentDistance < shortestDistance) 24 shortestDistance = currentDistance; 25 } 26 } 27 28 System.out.println("The shortest distance is " + shortestDistance); 29 } 30 31 public static double distance(double x1, double y1, double z1, double x2, double y2, double z2) 32 { 33 double square = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) + (z2 - z1) * (z2 - z1); 34 return Math.sqrt(square); 35 } 36 }
时间: 2024-10-07 04:17:38