HW7.7
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HW7.7相关的知识,希望对你有一定的参考价值。
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 }
以上是关于HW7.7的主要内容,如果未能解决你的问题,请参考以下文章