zoukankan      html  css  js  c++  java
  • Java进阶学习(3)之对象容器.小练习

    查找里程(10分)

    题目内容:

    下图为国内主要城市之间的公路里程:

    你的程序要读入这样的一张表,然后,根据输入的两个城市的名称,给出这两个城市之间的里程。

    注意:任何两个城市之间的里程都已经给出,不需要计算经第三地中转。

    注意:你并不需要去录入上图的数据,数据是在程序输入中给的。

    输入格式:

    首先,你会读到若干个城市的名字。每个名字都只是一个英文单词,中间不含空格或其他符号。当读到名字为“###”(三个#号)时,表示城市名字输入结束,###并不是一个城市的名字。如果记读到的城市名字的数量为n。

    然后,你会读到nxn的一个整数矩阵。第一行的每一个数字,表示上述城市名单中第一个城市依次到另一个城市之间的里程。表中同一个城市之间的里程为0。

    最后,你会读到两个城市的名字。

    输出格式:

    输出这两个城市之间的距离。

    输入样例:

    Hagzou Hugzou Jigxng    ###

    0 1108 708

    1108 0 994

    708 994 0

    Hagzou    Jigxng

    输出样例:

    708

    程序

     1 package com.mieba.notebook;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Scanner;
     5 
     6 public class SearchCity
     7 {
     8 
     9     private ArrayList<String> citys = new ArrayList<String>();
    10     private int[][] distance = new int[citys.size()][citys.size()];
    11 
    12     public boolean setCitys(String s)
    13     {
    14         if (s.equals("###"))
    15         {
    16             return false;
    17         } else
    18         {
    19             citys.add(s);
    20             return true;
    21         }
    22     }
    23 
    24     public void setDistance(int[][] a)
    25     {
    26         distance = a;
    27     }
    28 
    29     public int getDistance(String city1, String city2)
    30     {
    31         int i = getCityIndex(city1);
    32         int j = getCityIndex(city2);
    33         return distance[i][j];
    34     }
    35 
    36     public int getCityIndex(String city)
    37     {
    38         int b = -1;
    39         for (int i = 0; i < citys.size(); i++)
    40         {
    41             if (citys.get(i).equals(city))
    42             {
    43                 b = i;
    44                 break;
    45             }
    46         }
    47         return b;
    48     }
    49 
    50     public void getDistancelist()
    51     {
    52         for (int i = 0; i < distance.length; i++)
    53         {
    54             for (int j = 0; j < distance[i].length; j++)
    55             {
    56                 System.out.println(distance[i][j]);
    57             }
    58         }
    59     }
    60 
    61     public int getCitysLength()
    62     {
    63         return citys.size();
    64     }
    65     
    66     public static void main(String[] args)
    67     {
    68         Scanner in = new Scanner(System.in);
    69         SearchCity sc = new SearchCity();
    70         String s = new String();
    71         do
    72         {
    73             s = in.next();
    74         } while (sc.setCitys(s));
    75         int[][] a = new int[sc.getCitysLength()][sc.getCitysLength()];
    76         for (int i = 0; i < a.length; i++)
    77         {
    78             for (int j = 0; j < a[i].length; j++)
    79             {
    80                 a[i][j] = in.nextInt();
    81             }
    82         }
    83         sc.setDistance(a);
    84         System.out.println(sc.getDistance(in.next(), in.next()));
    85     }
    86 
    87 
    88 }

    运行结果

     总结:查找两个城市的里程,当我看见题目的第一个条件,城市的数量未知,那么数组和Hashmap都是不行的,题目的第二个条件,要完成查找,必须有序,所以我选择了ArrarList去存储城市,里程则需要用到对象的二维数组。

  • 相关阅读:
    shell函数
    sed命令
    交互式输入与for语句
    day01_云计算概述及kvm介绍
    grep与正则表达式
    shell的编程原理
    shell的文本处理工具
    shell的基础入门
    深入理解JavaScript中 fn() 和 return fn() 的区别
    一篇笔记带你快速掌握面向对象的Javascript(纯手打)
  • 原文地址:https://www.cnblogs.com/quxiangjia/p/12249585.html
Copyright © 2011-2022 走看看