-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercise08_21.java
51 lines (44 loc) · 1.89 KB
/
Exercise08_21.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.util.Scanner;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author jorda
*/
public class Exercise08_21 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of cities: ");
//create and populate the list of cities
double[][] cities = new double[input.nextInt()][2];
System.out.print("Enter the coordinates of the cities: ");
for (int cityNumber = 0; cityNumber < cities.length; cityNumber++) {
cities[cityNumber][0] = input.nextDouble();
cities[cityNumber][1] = input.nextDouble();
}
double shortestDistance = Double.MAX_VALUE;
int CentralCity = 0;
double CurrentDistance;
for (int fromCity = 0; fromCity < cities.length; fromCity++) {
CurrentDistance = 0;
for (int toCity = 0; toCity < cities.length; toCity++) {
CurrentDistance += distance(cities[fromCity][0],
cities[fromCity][1],
cities[toCity][0],
cities[toCity][1]);
}
if(CurrentDistance < shortestDistance){
shortestDistance = CurrentDistance;
CentralCity = fromCity;
}
}
System.out.println("The central city is at (" + cities[CentralCity][0] + ", " + cities[CentralCity][1] + ")");
System.out.println("The total distance to all other cities is " + shortestDistance);
}
public static double distance (double x1, double y1, double x2, double y2){
return Math.sqrt(Math.pow((x1 - x2), 2) + Math.pow((y1 - y2), 2));
}
}