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

HW7.8

时间:2016-08-30 13:39:00      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

 

 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

标签:

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

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