第八章第二十一题(中心城市)(key city)

第八章第二十一题(中心城市)(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
    
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值