码迷,mamicode.com
首页 > 其他好文 > 详细

HW7.7

时间:2016-08-30 12:10:04      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

 

 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

标签:

原文地址:http://www.cnblogs.com/wood-python/p/5821441.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!