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进阶_翁恺讲_第三周题的更多相关文章

  1. 第十讲_图像检索 Image Retrieval

    第十讲_图像检索 Image Retrieval 刚要 主要是图像预处理和特征提取+相似度计算 相似颜色检索 算法结构 颜色特征提取:统计图片的颜色成分 颜色特征相似度计算 色差距离 发展:欧式距离- ...

  2. 第九讲_图像生成 Image Captioning

    第九讲_图像生成 Image Captioning 生成式对抗网络 Generative Adversarial network 学习数据分布:概率密度函数估计+数据样本生成 生成式模型是共生关系,判 ...

  3. 第八讲_图像问答Image Question Answering

    第八讲_图像问答Image Question Answering 课程结构 图像问答的描述 具备一系列AI能力:细分识别,物体检测,动作识别,常识推理,知识库推理..... 先要根据问题,判断什么任务 ...

  4. 第七讲_图像描述(图说)Image Captioning

    第七讲_图像描述(图说)Image Captioning 本章结构 递归神经网络 时序后向传播(BPTT) 朴素Vanilla-RNN 基本模型 用sigmoid存在严重的梯度消失 LSTM长短时记忆 ...

  5. 第六讲_图像分割Image Segmentation

    第六讲_图像分割Image Segmentation 语义分割(semantic segmentation) 常用神经网络介绍对比-FCN SegNet U-net DeconvNet 目录 +三大数 ...

  6. 第四讲_图像识别之图像分类Image Classification

    第四讲_图像识别之图像分类Image Classification 目录 图片分类 性能指标:top1,top5 ILSVRC:每种任务数据集不一样 imageNet:根据WorldNet组织的图片集 ...

  7. 第二讲_图像数据处理Image Data Processing

    第二讲_图像数据处理Image Data Processing 深度模型出现后被弱化,但是思想的影子在深度模型中可以看到的 图片存储原理 RGB颜色空间:三通道(b,g,r),加法混色 CMY(K): ...

  8. Javascript Jquery 中的数组定义与操作_子木玲_新浪博客

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  9. c++拷贝构造函数(翁恺c++公开课[26-27]学习笔记)

    这节课在p26.拷贝构造中讲的很清楚,建议大家耐心的去看下. 什么时候会发生拷贝构造: 对象之间的初始化赋值 使用对象作为变量进行函数传参(通常使用引用来传参从而减去不必要的拷贝构造,提高效率和代码健 ...

随机推荐

  1. 缓存淘汰算法(LFU、LRU、ARC、FIFO、MRU)分析

    缓存算法是指令序列,用于决定缓存系统中哪些数据应该被删去. 常见类型包括LFU.LRU.ARC.FIFO.MRU. 一.最不经常使用算法(Least Frequently Used-LFU): 它是基 ...

  2. 【Pyton】【小甲鱼】异常处理:你不可能总是对的

    Exception 1.assertionerror举例 >>> my_list=['小甲鱼是帅哥'] >>> assert len(my_list)>0 & ...

  3. JavaWeb 服务启动时,在后台启动加载一个线程

    JavaWeb 服务启动时,在后台启动加载一个线程. 目前,我所掌握的一共有两种方法,第一种是监听(Listener),第二种是配置随项目启动而启动的Servlet. 下面对这两种方法做一简单的介绍, ...

  4. 复习一下property

    在面向对象程序里,一个对象不要直接访问另一个对象内部的数据.所以我们使用accessor methods来进行对象内部的数据交互. accessor methods(getters and sette ...

  5. Input消除自动记忆功能

    在html里就可以直接清除了<input type="text" autocomplete="off"> input 的autocomplete属性 ...

  6. js模拟电梯操作

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. JQuery表单元素过滤选择器

    此选择器主要是对所选择的表单元素进行过滤: 选择器 描述 返回 enabled 选择所有的可用的元素 集合元素 disabled 选择所有的不可用的元素 集合元素 checked 选择所有被选中的元素 ...

  8. 2.TypeScript 基础入门(二)

    变量类型的那些事 1.基本注解 类型注解使用:TypeAnnotation 语法.类型声明空间中可用的任何内容都可以用作类型注解. const num: number = 123; function ...

  9. linux 加减符号

    [root@LocalWeb01 ~]# aa=11[root@LocalWeb01 ~]# bb=22[root@LocalWeb01 ~]# cc=$aa+$bb[root@LocalWeb01 ...

  10. ef延迟加载不到导航属性问题

    最近做项目踩到了一个ef问题上的坑,导航属性(外键关键,如子表或主表等)“.”出来后是Null,外键值也对,数据库和ef的关系配置也都正确,就是加载不出来.后来发现实体里导航属性前少了个virtual ...