您现在的位置是:首页 > 文章详情

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

日期:2018-05-27点击:341
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

 

将编程看作是一门艺术,而不单单是个技术。 敲打的英文字符是我的黑白琴键, 思维图纸画出的是我编写的五线谱。 当美妙的华章响起,现实通往二进制的大门即将被打开。
原文链接:https://yq.aliyun.com/articles/638179
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章