地铁换乘 java_华为上机测试题(地铁换乘-java)

1 importjava.util.Scanner;2

3 public classStation {4

5 static String strA = "A1 A2 A3 A4 A5 A6 A7 A8 A9 T1 A10 A11 A12 A13 T2 A14 A15 A16 A17 A18";6 static String strB = "B1 B2 B3 B4 B5 T1 B6 B7 B8 B9 B10 T2 B11 B12 B13 B14 B15";7

8 static String[] SA = strA.split(" ");9 static String[] SB = strB.split(" ");10

11 static int LENSA =SA.length;12 static int LENSB =SB.length;13

14 public static voidmain(String[] args) {15

16 int step = 0;17 System.out.println("请输入\"始发站\"(空格)\"终到站\"(例如:A1 B4(回车结束)):");18 Scanner sc = newScanner(System.in);19 String[] strArray = sc.nextLine().split(" ");20

21 String x = strArray[0].toUpperCase();22 String y = strArray[1].toUpperCase();23

24 System.out.println("go:"+x);25 System.out.println("to:"+y);26

27 sc.close();28

29 step = getMinStep(x, y) + 1;//实际最小步数应该为getMinStep(x, y),此处按题示要求,所以结果加130 System.out.println("经过的最少车站数:"+step);31 }32

33 private static intgetMinStep(String x, String y) {34

35 if((‘A‘ != x.charAt(0))&&(‘A‘ != y.charAt(0)))36 {37 //在地铁B线路上,不用换乘,已经包含T1到T2

38 returnstepBtoB(x, y);39 }40 else if((‘B‘ != x.charAt(0))&&(‘B‘ != y.charAt(0)))41 {42 //在地铁A线路上

43 returnstepAtoA(x, y);44 }45 else

46 {47 //A到B,或者B到A

48 returnstepT1orT2(x, y);49 }50 }51

52 //从T1或者T2站换乘,s1到s2的最短距离

53 private static intstepT1orT2(String s1, String s2) {54 int lenXtoT1 =steptoT1(s1);55 int lenXtoT2 =steptoT2(s1);56 int lenYtoT1 =steptoT1(s2);57 int lenYtoT2 =steptoT2(s2);58

59 int lineT1 = lenXtoT1 +lenYtoT1;60 int lineT2 = lenXtoT2 +lenYtoT2;61

62 returnmin(lineT1, lineT2);63 }64

65 //到T1的最短距离

66 private static intsteptoT1(String s) {67

68 if(‘A‘ == s.charAt(0))69 {70 //找到s站在SA的下标

71 int i =getIndexofSA(s);72

73 //找到"T1"站在SA的下标

74 int j = getIndexofSA("T1");75

76 //找到"T2"站在SA的下标

77 int k = getIndexofSA("T2");78

79 //不换乘,s到T1最短路程

80 int line1 = min(mod(i, j), LENSA-mod(i, j));81 //不换乘,s到T2最短路程

82 int line2 = min(mod(i, k), LENSA-mod(i, k));83

84 return min(line1, line2+stepBtoB("T1", "T2"));85 }86 else

87 {88 return stepBtoB(s, "T1");89 }90 }91

92 //到T2的最短距离

93 private static intsteptoT2(String s) {94

95 if(‘A‘ == s.charAt(0))96 {97 //找到s站在SA的下标

98 int i =getIndexofSA(s);99

100 //找到"T1"站在SA的下标

101 int j = getIndexofSA("T1");102

103 //找到"T2"站在SA的下标

104 int k = getIndexofSA("T2");105

106 //不换乘,s到T1最短路程

107 int line1 = min(mod(i, j), LENSA-mod(i, j));108 //不换乘,s到T2最短路程

109 int line2 = min(mod(i, k), LENSA-mod(i, k));110

111 return min(line1+stepBtoB("T1", "T2"), line2);112 }113 else

114 {115 return stepBtoB(s, "T2");116 }117 }118

119 //A到A,有可能会从T1或者T2中转

120 private static intstepAtoA(String s1, String s2) {121 if((‘B‘ == s1.charAt(0))||(‘B‘ == s2.charAt(0)))122 {123 System.out.println("输入不是A线路上的站点,请检查!");124 return -1;125 }126

127 //找到s1站在SA的下标

128 int i =getIndexofSA(s1);129

130 //找到s2站在SA的下标

131 int j =getIndexofSA(s2);132

133 //不换乘,s1到s2的最短距离

134 int line1 = min(mod(i, j), LENSA-mod(i, j));135

136 //从T1或者T2站换乘,s1到s2的最短距离

137 int line2 =stepT1orT2(s1, s2);138

139 returnmin(line1, line2);140 }141

142 //B到B

143 private static intstepBtoB(String s1, String s2) {144 if((‘A‘ == s1.charAt(0))||(‘A‘ == s2.charAt(0)))145 {146 System.out.println("输入不是B线路上的站点,请检查!");147 return -1;148 }149

150 //找到s1站在SB的下标

151 int i =getIndexofSB(s1);152

153 //找到s2站在SB的下标

154 int j =getIndexofSB(s2);155

156 //取两数之模

157 returnmod(i, j);158 }159

160 private static int min(int a, intb)161 {162 return a

165 private static intgetIndexofSA(String str)166 {167 for(int index = 0; index < LENSA; index++)168 {169 if(str.equals(SA[index]))170 {171 returnindex;172 }173 }174

175 return -1;176 }177

178 private static intgetIndexofSB(String str)179 {180 for(int index = 0; index < LENSB; index++)181 {182 if(str.equals(SB[index]))183 {184 returnindex;185 }186 }187

188 return -1;189 }190

191 private static int mod(int a, intb)192 {193 if(a

198 {199 return a-b;200 }201 }202 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值