翁恺面向对象Java编程题答案——第三周

1查找里程(10分)
题目内容:

下图为国内主要城市之间的公路里程:
在这里插入图片描述
你的程序要读入这样的一张表,然后,根据输入的两个城市的名称,给出这两个城市之间的里程。

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

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

输入格式:

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

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

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

输出格式:

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

输入样例:

Hagzou Hugzou Jigxng ###

0 1108 708

1108 0 994

708 994 0

Hagzou Jigxng

输出样例:

708

时间限制:500ms内存限制:32000kb

import java.util.ArrayList;
import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		ArrayList<String> notes = new ArrayList<String>();
		String position, destination;
		int pstIndex, dstIndex;
		
		String str = in.next();
		while (str.equals("###") != true) {
			notes.add(str);
			str = in.next();
		}
		
		final int arraySize = notes.size();
		int[] distance = new int[arraySize * arraySize];
		for (int i = 0; i < distance.length; i++) {
			distance[i] = in.nextInt();
		}
		
		position = in.next();
		destination = in.next();
		pstIndex = notes.indexOf(position);
		dstIndex = notes.indexOf(destination);
		System.out.println(distance[pstIndex*arraySize+dstIndex]);
		in.close();
	}
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值