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

PS:自己写的,自测试OK,供大家参考。

/*

高级题样题:地铁换乘
描述:已知2条地铁线路,其中A为环线,B为东西向线路,线路都是双向的。经过的站点名分别如下,两条线交叉的换乘点用T1、T2表示。
编写程序,任意输入两个站点名称,输出乘坐地铁最少需要经过的车站数量(含输入的起点和终点,换乘站点只计算一次)。
地铁线A(环线)经过车站:A1 A2 A3 A4 A5 A6 A7 A8 A9 T1 A10 A11 A12 A13 T2 A14 A15 A16 A17 A18
地铁线B(直线)经过车站:B1 B2 B3 B4 B5 T1 B6 B7 B8 B9 B10 T2 B11 B12 B13 B14 B15
输入:输入两个不同的站名
输出:输出最少经过的站数,含输入的起点和终点,换乘站点只计算一次
输入样例:A1 A3
输出样例:3

(注意:按照题示,A1 A3结果为3,所以A1 A1结果就应该为1,依此。)
*/

  1 import java.util.Scanner;
  2 
  3 public class Station {
  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     static int indexT1SA = getIndexofSA("T1");    //"T1"车站在A线路上的数组下标
 15     static int indexT1SB = getIndexofSB("T1");    //"T1"车站在B线路上的数组下标
 16     static int indexT2SA = getIndexofSA("T2");    //"T2"车站在A线路上的数组下标
 17     static int indexT2SB = getIndexofSB("T2");    //"T2"车站在B线路上的数组下标
 18     
 19     public static void main(String[] args) {
 20         
 21         int step = 0;
 22         System.out.println("请输入\"始发站\"(空格)\"终到站\"(例如:A1 B4(回车结束)):");
 23         Scanner sc = new Scanner(System.in);
 24         String[] strArray = sc.nextLine().split(" ");
 25         
 26         String x = strArray[0].toUpperCase();
 27         String y = strArray[1].toUpperCase();
 28         
 29         System.out.println("go:"+x);
 30         System.out.println("to:"+y);
 31         
 32         sc.close();
 33         
 34         step = getMinStep(x, y) + 1;
 35         System.out.println("经过的最少车站数:"+step);
 36     }
 37 
 38     private static int getMinStep(String x, String y) {
 39         
 40         if(('A' != x.charAt(0))&&('A' != y.charAt(0)))
 41         {
 42             //在地铁B线路上
 43             return min(stepBtoB(x, y), stepT1orT2(x, y));
 44         }
 45         else if(('B' != x.charAt(0))&&('B' != y.charAt(0)))
 46         {
 47             //在地铁A线路上
 48             return min(stepAtoA(x, y), stepT1orT2(x, y));
 49         }
 50         else
 51         {
 52             //A到B,或者B到A
 53             return stepT1orT2(x, y);
 54         }
 55     }
 56 
 57     //从T1或者T2站换乘,s1到s2的最短距离
 58     private static int stepT1orT2(String s1, String s2) {
 59         int lenXtoT1 = steptoT1(s1);
 60         int lenXtoT2 = steptoT2(s1);
 61         int lenYtoT1 = steptoT1(s2);
 62         int lenYtoT2 = steptoT2(s2);
 63         
 64         int lineT1 = lenXtoT1 + lenYtoT1;
 65         int lineT2 = lenXtoT2 + lenYtoT2;
 66         
 67         return min(lineT1, lineT2);
 68     }
 69 
 70     //到T1的最短距离
 71     private static int steptoT1(String s) {
 72         
 73         if("T1".equals(s))
 74         {
 75             return 0;
 76         }
 77         else if("T2".equals(s))
 78         {
 79             return min(stepAtoA("T1", "T2"), stepBtoB("T1", "T2"));
 80         }
 81         else if('A' == s.charAt(0))    //s是A线路上的车站
 82         {
 83             //找到s站在SA的下标
 84             int indexSSA = getIndexofSA(s);
 85             
 86             //不换乘,s到T1最短路程
 87             int line1 = min(mod(indexSSA, indexT1SA), LENSA-mod(indexSSA, indexT1SA));
 88             //不换乘,s到T2最短路程
 89             int line2 = min(mod(indexSSA, indexT2SA), LENSA-mod(indexSSA, indexT2SA));
 90             
 91             return min(line1, line2+min(stepAtoA("T1", "T2"), stepBtoB("T1", "T2")));
 92         }
 93         else if('B' == s.charAt(0))    //s是B线路上的车站
 94         {
 95             //找到s站在SB的下标
 96             int indexSSB = getIndexofSB(s);
 97             
 98             //不换乘,s到T1最短路程
 99             int line1 = mod(indexSSB, indexT1SB);
100             //不换乘,s到T2最短路程
101             int line2 = mod(indexSSB, indexT2SB);
102             
103             return min(line1, line2+min(stepAtoA("T1", "T2"), stepBtoB("T1", "T2")));
104         }
105         else
106         {
107             System.out.println("车站名有误,请检查!");
108             return -1;
109         }
110     }
111 
112     //s到T2的最短距离
113     private static int steptoT2(String s) {
114 
115         if("T2".equals(s))
116         {
117             return 0;
118         }
119         else if("T1".equals(s))
120         {
121             return min(stepAtoA("T1", "T2"), stepBtoB("T1", "T2"));
122         }
123         else if('A' == s.charAt(0))    //s是A线路上的车站
124         {
125             //找到s站在SA的下标
126             int indexSSA = getIndexofSA(s);
127             
128             //不换乘,s到T1最短路程
129             int line1 = min(mod(indexSSA, indexT1SA), LENSA-mod(indexSSA, indexT1SA));
130             //不换乘,s到T2最短路程
131             int line2 = min(mod(indexSSA, indexT2SA), LENSA-mod(indexSSA, indexT2SA));
132             
133             return min(line2, line1+min(stepAtoA("T1", "T2"), stepBtoB("T1", "T2")));
134         }
135         else if('B' == s.charAt(0))    //s是B线路上的车站
136         {
137             //找到s站在SB的下标
138             int indexSSB = getIndexofSB(s);
139             
140             //不换乘,s到T1最短路程
141             int line1 = mod(indexSSB, indexT1SB);
142             //不换乘,s到T2最短路程
143             int line2 = mod(indexSSB, indexT2SB);
144             
145             return min(line2, line1+min(stepAtoA("T1", "T2"), stepBtoB("T1", "T2")));
146         }
147         else
148         {
149             System.out.println("车站名有误,请检查!");
150             return -1;
151         }
152     }
153 
154     //A到A,不换乘
155     private static int stepAtoA(String s1, String s2) {
156         if(('B' == s1.charAt(0))||('B' == s2.charAt(0)))
157         {
158             System.out.println("输入不是A线路上的站点,请检查!");
159             return -1;
160         }
161         
162         //找到s1站,在A线路上的数组下标
163         int indexS1SA = getIndexofSA(s1);
164         
165         //找到s2站,在A线路上的数组下标
166         int indexS2SA = getIndexofSA(s2);
167         
168         //不换乘,s1到s2的最短距离
169         return min(mod(indexS1SA, indexS2SA), LENSA-mod(indexS1SA, indexS2SA));
170     }
171 
172     //B到B,不换乘
173     private static int stepBtoB(String s1, String s2) {
174         if(('A' == s1.charAt(0))||('A' == s2.charAt(0)))
175         {
176             System.out.println("输入不是B线路上的站点,请检查!");
177             return -1;
178         }
179         
180         //找到s1站,在B线路上的数组下标
181         int indexS1SB = getIndexofSB(s1);
182         
183         //找到s2站,在B线路上的数组下标
184         int indexS2SB = getIndexofSB(s2);
185         
186         //不换乘,s1到s2的最短距离
187         return mod(indexS1SB, indexS2SB);
188     }
189     
190     private static int min(int a, int b)
191     {
192         return a<b?a:b;
193     }
194 
195     private static int getIndexofSA(String str)
196     {
197         for(int index = 0; index < LENSA; index++)
198         {
199             if(str.equals(SA[index]))
200             {
201                 return index;
202             }
203         }
204         
205         return -1;
206     }
207 
208     private static int getIndexofSB(String str)
209     {
210         for(int index = 0; index < LENSB; index++)
211         {
212             if(str.equals(SB[index]))
213             {
214                 return index;
215             }
216         }
217         
218         return -1;
219     }
220     
221     private static int mod(int a, int b)
222     {
223         if(a < b)
224         {
225             return b-a;
226         }
227         else
228         {
229             return a-b;
230         }
231     }
232 }

 

posted @ 2014-09-25 01:05  lr_ting  阅读(4941)  评论(3编辑  收藏  举报
simple statistics
Econo Lodge Coupons