资源描述:
《华为机试-地铁换乘.pdf》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、已知2条地铁线路,其中A为环线,B为东西向线路,线路都是双向的。经过的站点名分别如下,两条线交叉的换乘点用T1、T2表示。编写程序,任意输入两个站点名称,输出乘坐地铁最少需要经过的车站数量(含输入的起点和终点,换乘站点只计算一次)。地铁线A(环线)经过车站:A1A2A3A4A5A6A7A8A9T1A10A11A12A13T2A14A15A16A17A18地铁线A(直线)经过车站:B1B2B3B4B5T1B6B7B8B9B10T2B11B12B13B14B15输入:输入两个不同的站名输出:输出最少经过的站数,含输入的起点和终点,换乘站点只计
2、算一次packagehuaweiTest.Third;importjava.util.Scanner;publicclassMain{/***@paramargs*/publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubString[]trainA={"A1","A2","A3","A4","A5","A6","A7","A8","A9","T1","A10","A11","A12","A13","T2","A14","A15","A16","A17","A18"
3、};String[]trainB={"B1","B2","B3","B4","B5","T1","B6","B7","B8","B9","B10","T2","B11","B12","B13","B14","B15"};Scannersc=newScanner(System.in);Stringstart=sc.next();Stringend=sc.next();intlength=0;//char[]startChar=start.toCharArray();//char[]endChar=end.toCharArray();if((
4、start.charAt(0)==end.charAt(0)&&start.charAt(0)!='T')
5、
6、(start.charAt(0)=='T'&&end.charAt(0)!='T')
7、
8、(start.charAt(0)!='T'&&end.charAt(0)=='T'))length=theSameTrainDistance(start,end,trainA,trainB);elseif(start.charAt(0)=='T'&&end.charAt(0)=='T'){length=6;}else{intproject1=t
9、heSameTrainDistance(start,"T1",trainA,trainB)+theSameTrainDistance("T1",end,trainA,trainB);intproject2=theSameTrainDistance(start,"T2",trainA,trainB)+theSameTrainDistance("T2",end,trainA,trainB);if(project1>project2)length=project2;elselength=project1;}System.out.println(
10、++length);}privatestaticinttheSameTrainDistance(Stringstart,Stringend,String[]trainA,String[]trainB){intlength=0;if((start.charAt(0)=='A'
11、
12、start.charAt(0)=='T')&&(end.charAt(0)=='A'
13、
14、end.charAt(0)=='T')){intmarkStart=0,markEnd=0;for(inti=0;i15、].equals(start))markStart=i;if(trainA[i].equals(end))markEnd=i;}if(Math.abs(markEnd-markStart)<=11)length=Math.abs(markEnd-markStart);elselength=trainA.length+1-Math.abs(markEnd-markStart);}if((start.charAt(0)=='B'
16、
17、start.charAt(0)=='T')&&(end.charAt(0)=='B'
18、
19、end.charAt(0
20、)=='T')){intmarkStart=0,markEnd=0;for(inti=0;i