HW7.8

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的更多相关文章
- HW7.18
public class Solution { public static void main(String[] args) { int[][] m = {{1, 2}, {3, 4}, {5, 6} ...
- HW7.17
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- HW7.16
import java.util.Arrays; public class Solution { public static void main(String[] args) { int row = ...
- HW7.15
public class Solution { public static void main(String[] args) { double[][] set1 = {{1, 1}, {2, 2}, ...
- HW7.14
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- HW7.13
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- HW7.12
import java.util.Scanner; public class Solution { public static void main(String[] args) { double[] ...
- HW7.11
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- HW7.10
public class Solution { public static void main(String[] args) { int[][] array = new int[3][3]; for( ...
- HW7.9
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
随机推荐
- PHP session有效期session.gc_maxlifetime的设置方法
PHP(外文名: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言.语法吸收了C语言.Java和Perl的特点,入门门槛较低,易于学习,使用广泛,主要适 ...
- UNITY3D使用NGUI制作自适应UI的总结
原地址:http://www.cnitblog.com/updraft/archive/2013/11/12/88801.html 制作自适应的几个方法1. 使用 UIROOT 里设置自定义高度的方法 ...
- Binding to the Most Recent Visual Studio Libraries--说的很详细,很清楚
Every version of Visual Studio comes with certain versions of the Microsoft libraries, such as the C ...
- POJ1860——Currency Exchange(BellmanFord算法求最短路)
Currency Exchange DescriptionSeveral currency exchange points are working in our city. Let us suppos ...
- 安装Ubuntu双系统系列——安装中文输入法
Ubuntu 12.04中文输入法的安装 Ubuntu上的输入法主要有小小输入平台(支持拼音/二笔/五笔等),Fcitx,Ibus,Scim等.其中Scim和Ibus是输入法框架.在Ubuntu的中文 ...
- 一篇文章教会你,如何做到招聘要求中的“要有扎实的Java基础
来历 本文来自于一次和群里猿友的交流,具体的情况且听LZ慢慢道来. 一日,LZ在群里发话,“招人啦.” 然某群友曰,“群主,俺想去.” LZ回之,“你年几何?” 群友曰,“两年也.” LZ憾言之,“惜 ...
- fork、vfork、clone区别
在Linux中主要提供了fork.vfork.clone三个进程创建方法. 问题 在linux源码中这三个调用的执行过程是执行fork(),vfork(),clone()时,通过一个系统调用表映射到s ...
- JNDI和在tomcat中配置DBCP连接池 元数据的使用 DBUtils框架的使用 多表操作
1 JNDI和在tomcat中配置DBCP连接池 JNDI(Java Naming and Directory Interface),Java命名和目录接口,它对应于J2SE中的javax.namin ...
- js设置datagriad的行移动
// ,// formatter: function(value,row,index){// ...
- Unity3D之如何创建正确的像素比在屏幕上
关于这篇文章的命名,实在不知道怎么命名好,大概功能就是:比如一张宽高为100x100的图片显示在屏幕上,那2D摄像头的Size值为多少时,屏幕上显示出来图片大小和图片的实际像素一致. 这里涉及到一个G ...