import java.util.ArrayList;
 import java.util.Scanner;

 public class Solution
 {
     public static void main(String[] args)
     {
         ArrayList<String> list = new ArrayList<>();
         Scanner input = new Scanner(System.in);
         System.out.print("Enter the count of points: ");
         int pointCount = input.nextInt();
         double[][] points = new double[pointCount][2];

         System.out.print("Enter the points: ");
         for(int i = 0; i < pointCount; i++)
             for(int j = 0; j < 2; j++)
                 points[i][j] = input.nextDouble();

         input.close();

         double shortestDistance = distance(points[0][0], points[0][1], points[1][0], points[1][1]);
         double currentDistance = shortestDistance;

         for(int i = 0; i < points.length; i++)
             for(int j = i + 1; j < points.length; j++)
             {
                 currentDistance = distance(points[i][0], points[i][1], points[j][0], points[j][1]);
                 if(currentDistance < shortestDistance)
                 {
                     list.clear();
                     shortestDistance = currentDistance;
                     list.add("(" + points[i][0] + " " + points[i][1] + ") (" + points[j][0] + " " + points[j][1] + ")");
                 }
                 else if(currentDistance == shortestDistance)
                 {
                     list.add("(" + points[i][0] + " " + points[i][1] + ") (" + points[j][0] + " " + points[j][1] + ")");
                 }
             }

         System.out.println("The shortest distance is " + shortestDistance);
         for(int i = 0; i < list.size(); i++)
             System.out.println(list.get(i));
     }

     public static double distance(double x1, double y1, double x2, double y2)
     {
         double square = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
         return Math.sqrt(square);
     }
 }

HW7.8的更多相关文章

  1. HW7.18

    public class Solution { public static void main(String[] args) { int[][] m = {{1, 2}, {3, 4}, {5, 6} ...

  2. HW7.17

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  3. HW7.16

    import java.util.Arrays; public class Solution { public static void main(String[] args) { int row = ...

  4. HW7.15

    public class Solution { public static void main(String[] args) { double[][] set1 = {{1, 1}, {2, 2}, ...

  5. HW7.14

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  6. HW7.13

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  7. HW7.12

    import java.util.Scanner; public class Solution { public static void main(String[] args) { double[] ...

  8. HW7.11

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  9. HW7.10

    public class Solution { public static void main(String[] args) { int[][] array = new int[3][3]; for( ...

  10. HW7.9

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

随机推荐

  1. Scala中的Extractor

    Scala中使用unapply方法可以实现三种extractor(另外使用unapplySeq也可以实现extractor) def unapply(object: S): Option[(T1, . ...

  2. C++11的for循环,以及范围Range类的实现

    C++11支持range-based for循环.这是一个很方便的特性,能省挺多代码.以下代码就能很方便的遍历vector中的元素,并打印出来: 1 2 3 4 5 6 7 8 std::vector ...

  3. 扩展DJANGO的LISTVIEW

    不用MODEL,不用QUERYSET,而用get_queryset方法来扩展LISTVIEW, 从而实现特定过滤或搜索功能. class DVListView(ListView): template_ ...

  4. jquery的ajax向后台servlet传递json类型的多维数组

    后台运行结果:                                                                                      前台运行结果: ...

  5. java Collections.sort()实现List排序的默认方法和自定义方法

    1.java提供的默认list排序方法 主要代码: List<String> list = new ArrayList();list.add("刘媛媛"); list. ...

  6. 编程实现Windows关机、重启、注销

    要想编程使Windows关机.重启或者注销,可以使用ExWindowsEx这个API函数,该函数只有两个参数,第一个表示关机动作的标志,也就是你要让该函数关机呢,还是重启,还是注销等.可以使用EWX_ ...

  7. *J2EE中乱码处理

    发生中文乱码有三种情况 表单form (1)post 首先确定浏览器的编码方式,比如说utf-8,请求发给web服务器,web服务器以编码方式iso-9959-1来接收数据(服务器是外国人编写的),服 ...

  8. HDU4908——BestCoder Sequence(BestCoder Round #3)

    BestCoder Sequence Problem DescriptionMr Potato is a coder.Mr Potato is the BestCoder.One night, an ...

  9. 如何修改linux系统主机名称

    完成目标: 修改centos 7系统的主机名称 使用命令: hostnamectl [root@ossec-server ~]# hostnamectl --help hostnamectl [OPT ...

  10. 安装Hadoop系列 — 安装JDK-8u5

    安装步骤如下: 1)下载 JDK 8 从http://www.oracle.com/technetwork/java/javasebusiness/downloads/ 选择下载JDK的最新版本 JD ...