MOOC_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
将编程看作是一门艺术,而不单单是个技术。 敲打的英文字符是我的黑白琴键, 思维图纸画出的是我编写的五线谱。 当美妙的华章响起,现实通往二进制的大门即将被打开。
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
现代化自定制 - 页面上的自定制
博客地址:http://blog.csdn.net/FoxDave本篇接上一篇,阐述一下关于页面上自定制的内容要如何进行现代化转换时的修复。 页面上的自定制 SharePoint有很多种经典网站页面如wiki、web部件页、发布页面等。每种页面都允许用户进行自定制,比如添加web部件、格式化的文本或嵌入JavaScript来实现自定义操作等。 拆箱即用的经典web部件SharePoint有很多拆箱即用的web部件(包括脚本编辑器web部件),它们在经典页面工作,但是并不支持现代化页面。也就是说,没有完全对等的客户端web部件来跟传统经典web部件一一对应。如果我们需要经典web部件的功能作为客户端的等价实现,我们必须自己开发基于SharePoint Framework的客户端web部件,我们可以从GitHub上找到web部件库。下图是一个web部件示例。 基于网站集或网站的嵌入JavaScript的自定义操作在之前的介绍中,我们提到过常用的添加网站展示条的模式是通过嵌入JavaScript实现自定义操作。但这种是不兼容现代化用户接口的,我们必须基于SharePoint Framewor...
- 下一篇
一文读懂目标检测模型(附论文资源)
1. R-CNN R-CNN是2014年出现的。它是将CNN用于对象检测的起源,能够基于丰富的特征层次结构进行目标精确检测和语义分割来源。 如何确定这些边界框的大小和位置呢?R-CNN网络是这样做的:在图像中提出了多个边框,并判断其中的任何一个是否对应着一个具体对象。 要想进一步了解,可以查看以下PPT和笔记: http://www.image-net.org/challenges/LSVRC/2013/slides/r-cnn-ilsvrc2013-workshop.pdf http://www.cs.berkeley.edu/~rbg/slides/rcnn-cvpr14-slides.pdf http://zhangliliang.com/2014/07/23/paper-note-rcnn/ 2. Fast R-CNN 2015年,R-C
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- Docker安装Oracle12C,快速搭建Oracle学习环境
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- CentOS6,CentOS7官方镜像安装Oracle11G
- CentOS关闭SELinux安全模块
- CentOS7设置SWAP分区,小内存服务器的救世主
- CentOS7,CentOS8安装Elasticsearch6.8.6
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装