MOOC_Java进阶_翁恺讲_第三周题

java

package mooc_java进阶_d3周题;

/**

* 没有使用HashMap

*/

import java.util.ArrayList;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

String stopSymbol = "###";

int cityNumbers = 0;

boolean goOn = true;

ArrayList<String> citys = new ArrayList<String>();

while (goOn) {

String cityOnly = in.next();

if (cityOnly.equals(stopSymbol)) {

break;

} else {

citys.add(cityOnly);

}

}

cityNumbers = citys.size();

int n = cityNumbers;

Scanner in1 = new Scanner(System.in);

int allN = n * n;

int[] matrix = new int[allN];

for (int i = 0; i < matrix.length; i++) {

matrix[i] = in1.nextInt();

}

Scanner in3 = new Scanner(System.in);

String[] twoCitys = new String[2];

twoCitys[0] = in3.next();

twoCitys[1] = in3.next();

int count = 0;

int firstCityIndex = 0;

int secondCityIndex = 0;

for (int j = 0; j < 2; j++) {

for (int i = 0; i < n; i++) {

if (citys.get(i).equals(twoCitys[j])) {

count++;

if (count == 1) {

firstCityIndex = i;

} else if (count == 2) {

secondCityIndex = i;

} else {

}

}

}

}

int leftCityIndex = 0;

int rightCityIndex = 0;

int city_D_value = 0;

int li;

int liInMat;

if (firstCityIndex < secondCityIndex) {

leftCityIndex = firstCityIndex;

li = leftCityIndex;

liInMat = li * n + li;

city_D_value = secondCityIndex - firstCityIndex;

rightCityIndex = liInMat + city_D_value;

} else if (firstCityIndex > secondCityIndex) {

leftCityIndex = secondCityIndex;

li = leftCityIndex;

liInMat = li * n + li;

city_D_value = firstCityIndex - secondCityIndex;

rightCityIndex = liInMat + city_D_value;

} else {

}

int distance = matrix[rightCityIndex];

if (distance != 0) {

System.out.print(distance);

} else {

System.out.println("0");

}

in.close();

}

}

上面这个没有通过在线验证,不过我本机试着还行.可能是有些问题吧,写得太杂了

之后看了其他人用HashMap实现的,我尝试了下,不过最终还是比他们大神写的要长很多:不过也很满足已经通过了测验:

package mooc_java进阶_d3周题;

/**

* 使用了HashMap

*/

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Map;

import java.util.Scanner;

public class Main4 {

public static void main(String[] args) {

Main4 m = new Main4();

// STEP 1 : 用基础方法去模拟方法实现

Scanner in = new Scanner(System.in);

ArrayList<String> citys = new ArrayList<String>();

boolean go = true;

String oneOfcity;

String stopSymbol = "###";

while (go) {

oneOfcity = in.next();

if (oneOfcity.equals(stopSymbol)) {// 如果这轮输入的单个城市是###休止符

break;//

}

citys.add(oneOfcity);

}

int n = citys.size();// 获取城市数量

int numOfCitys = n * n;// 获取城市矩阵距离数量

// STEP 2 : 获取矩阵数字

ArrayList<Integer> matrixNumber = new ArrayList<Integer>();

int count = 0;

Integer x;

Integer xx = null;

while (go) {

x = in.nextInt();

matrixNumber.add(x);// 接收矩阵数字

count++;

if (count == numOfCitys) {

break;

}

}

// STEP 3 : 获取最后的两个城市 调用getCitysGroup方法

String lastTwoCitys = m.getCitysGroup(in.next(), in.next());

// STEP 4 : 返回的Map是拼接的城市名字符串以及Integer矩阵数字

Map all = m.getHashCity(citys, matrixNumber);

Integer result = (Integer) all.get(lastTwoCitys);

System.out.println(result);

}

//怎样创建一个方法接受两个方法传入的动态数据

//接收citys 和 矩阵

private Map getHashCity(ArrayList<String> list,ArrayList<Integer> matrix) {

String hashcity = null;

Integer matNumbers = null;

Map<String, Integer> hashcitys = new HashMap<>();

int numCount = 0;

for (int i = 0; i < list.size(); i++) {

for (int j = 0; j < list.size(); j++) {

hashcity = getCitysGroup(list.get(i), list.get(j));

matNumbers = matrix.get(numCount);

hashcitys.put(hashcity, matNumbers);

numCount++;

}

}

return hashcitys;

}

private String getCitysGroup(String one, String two) {

// hashString现在是城市组合的相加的字符串

String hashString = one + two;

return hashString;

}

}

https://www.icourse163.org/learn/ZJU-1001542001#/learn/ojhw?id=1003683048

以上是 MOOC_Java进阶_翁恺讲_第三周题 的全部内容, 来源链接: utcz.com/z/390107.html

回到顶部