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 ...
随机推荐
- spoj 379
题是水题 但丫的题目意思太难懂 ....... 英语水平 ...... #include <cstdio> #include <cstring> #include &l ...
- A JSTL primer, Part 2: Getting down to the core
In the initial article of this series, you got your first look at JSTL. We described the use of its ...
- eclipse进行开发
最近在用eclipse进行开发的时候遇到了一个很奇怪的问题,其实这个问题很早以前就遇到了只是苦于一直没有需找到答案.直到今天又遇到了,才觉得这真是个很实用很使用的功能,所以分享给大家,希望对大家有帮助 ...
- objective-c宏定义
1.先来几个常用的: // 是否高清屏 #define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? ...
- ids & hdmi 原理
http://www.taiwanwebinar.com/zh_TW/STATIC/SITE/dwc_hdmi_tx.pdf http://blog.csdn.net/g_salamander/art ...
- SPRING IN ACTION 第4版笔记-第九章Securing web applications-010-拦截请求
一. What if you wanted to restrict access to certain roles only on Tuesday? Using the access() method ...
- Oracle程序包
程序包由两部分构成:规范(specification)和主体(body). 创建表 create table PEOPLE ( ID NUMBER primary key not null, NAME ...
- Android EditText属性
1.EditText输入的文字为密码形式的设置 (1)通过.xml里设置: 把该EditText设为:android:password="true" // 以”.”形式显示文本 ( ...
- No ongoing transaction. Did you forget to call multi?
2016-10-21 14:41:47,551 [ERROR] [http-nio-8032-exec-2] TransactionSynchronizationUtils:171 - Transac ...
- Oracle 列顺序测试
列顺序测试 大家在做表设计的时候通常对表中列的排列顺序没有过多注意,但是其实越常用的列,它的位置越靠前,则查询速度越快. 因为每个block里面存储了row directory (每行数据在块中的位移 ...