第八章第二十一题(中心城市)(key city)
-
*8.21(中心城市)给定一组城市,中心城市是和所有其他城市之间具有最短距离的城市。编写一个程序,提示用户输入城市的数目以及城市的位置(坐标),找到中心城市以及和所有其他城市的总距离。
Enter the number of cities:5
Enter the coordinates of the cities:
2.5 5 5.1 3 1 9 5.4 54 5.5 2.1
The central city is at(2.5,5.0)
The total distance to all other cities is 60.81
*8.21(key city)Given a group of cities, the central city is the city with the shortest distance from all other cities. Write a program to prompt the user to enter the number of cities and the location (coordinates) of cities, find the central city and the total distance from all other cities.
Enter the number of cities:5
Enter the coordinates of the cities:
2.5 5 5.1 3 1 9 5.4 54 5.5 2.1
The central city is at(2.5,5.0)
The total distance to all other cities is 60.81 -
参考代码:
package chapter08; import java.util.Scanner; public class Code_21 { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.print("Enter the number of cities:"); int numberOfCities=input.nextInt(); System.out.println("Enter the coordinates of the cities:"); double[][] coordinates=new double[5][2]; for (int i=0;i<coordinates.length;i++){ //输入坐标 for (int j=0;j<coordinates[i].length;j++){ coordinates[i][j]=input.nextDouble(); } } double totalDistance=totalDistance(coordinates,0); double shortestDistance=totalDistance; int cityNumber=0; for(int i=0;i<coordinates.length;i++){ //比较每一个城市的总距离 if (totalDistance(coordinates,i)<totalDistance) { shortestDistance = totalDistance(coordinates, i); cityNumber = i; } } System.out.println("The " + "central city is at("+coordinates[cityNumber][0]+","+coordinates[cityNumber][1]+")"); System.out.printf("The total distance to all other cities is %.2f",shortestDistance); } public static double totalDistance(double[][] cities,int cityNumber){ double totalDistance=0; for(int i=0;i<cities.length;i++){ double distance = Math.sqrt((cities[i][0] -cities[cityNumber][0]) * (cities[i][0] - cities[cityNumber][0]) + (cities[i][1] - cities[cityNumber][1]) * (cities[i][1] - cities[cityNumber][1])); totalDistance+=distance; } return totalDistance; } }
-
结果显示:
Enter the number of cities:5 Enter the coordinates of the cities: 2.5 5 5.1 3 1 9 5.4 54 5.5 2.1 The central city is at(2.5,5.0) The total distance to all other cities is 60.81 Process finished with exit code 0