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 ...
随机推荐
- C语言:将16进制字符串转化为int类型值
将16进制字符串值转换为 int 整型值 此例中用 "1de" 作为测试字符串,实现代码如下: #include <stdio.h> #include <stdl ...
- http://www.ibm.com/developerworks/cn/java/j-lo-hotswapcls/
http://www.ibm.com/developerworks/cn/java/j-lo-hotswapcls/
- codeforces #305 B Mike and Feet
跟之前做过的51Nod的移数博弈是一样的QAQ 我们考虑每个数的贡献 定义其左边第一个比他小的数的位置为L 定义其右边第一个比他小的数的位置为R 这个可以用排序+链表 或者 单调队列 搞定 那么对于区 ...
- Java List详解
就是一种集合对象,将所有的对象集中到一起存储. list里面可以放java对象,可以直接放值. List list = new ArrayList(); list.add("AAA" ...
- 96. Unique Binary Search Trees
题目: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For e ...
- WPF控件模板和数据模板
来自:http://www.th7.cn/Program/WPF/2011/12/21/51676.shtml ControlTemplate用于描述控件本身. 使用TemplateBinding来绑 ...
- uva 11292 Dragon of Loowater (勇者斗恶龙)
Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance tur ...
- 【HDOJ】1448 The Treasure
这就是个简单的bfs.真没什么好说的,三维的状态就可以了.每次预处理一下monster的位置,然后再恢复. /* 1924 */ #include <iostream> #include ...
- Task的使用
在.net4.0的时候推出的Task using System; using System.Threading; using System.Threading.Tasks; namespace Tas ...
- 推荐:ThoughtWorks(中国)程序员读书雷达
部分转自张逸的博客:http://agiledon.github.io/blog/2013/04/17/thoughtworks-developer-reading-radar/ 长久以来一直对程序员 ...