码迷,mamicode.com
首页 > 其他好文 > 详细

地铁票价计算程序

时间:2015-05-26 00:07:54      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:

根据某市地铁线路图写一个地铁票价计算程序
 
需求描述: 

1.计费规则:最低2元,超过5站以上每站加收0.5元,换乘重新起算,例如L1先坐4站,换乘L2再坐6站,结果就是2+2.5=5.5元
 
2.程序启动以后读取输入文件(in.txt),内容格式如:
 
L2-8,L2-2
 
X3,L3-8
 
....
 
每行表示一次行程,起点站和终点站之间用逗号分隔,行数不限
 
4.系统按最短路径方案(尽量少换乘且站数少,假设乘 客换乘一次用的时间相当于坐4个站)规划路线,计算票价,并把路线和票价输出到文件(out.txt),内容格式如:
 
L2-8,L2-2=2.5:L2-8,L2-7,L2-6,L2-5,L2-4,L2-3,L2-2
 
X3,L3-8=4:X3,X4,L3-8
 
....
 
等号后面的表示票价和路径
 


地铁线路图如下:共有5条线路,X开头的站点表示 换乘车站
技术分享

  1 package com.test.test;
  2 
  3 import java.util.HashMap;
  4 import java.util.HashSet;
  5 import java.util.Iterator;
  6 import java.util.Map;
  7 import java.util.Set;
  8 
  9 public class Test3 {
 10     static Map<Integer,Station> stationMap=new HashMap<Integer,Station>();
 11     public static void main(String[] args) {
 12         
 13         
 14         Station station1=new Station(1,"1号线第一站",true,false);
 15         Station station2=new Station(1,"1号线第二站",false,false);
 16         station1.setNextStation(station2);
 17         Station station3=new Station(1,"第三站",false,true);
 18         station3.addTransferStations(2);
 19         station2.setNextStation(station3);
 20         Station station4=new Station(1,"1号线第四站",true,false);
 21         station3.setNextStation(station4);
 22         
 23         
 24         Station station21=new Station(2,"2号线第一站",true,false);
 25         Station station22=new Station(2,"2号线第二站",false,false);
 26         station21.setNextStation(station22);
 27         Station station23=new Station(2,"第三站",false,true);
 28         station23.addTransferStations(1);
 29         station22.setNextStation(station23);
 30         Station station24=new Station(2,"2号线第四站",true,false);
 31         station23.setNextStation(station24);
 32         
 33         stationMap.put(1, station1);
 34         stationMap.put(1, station21);
 35         
 36         int n=getPosition(station1, "1号线第一站", "1号线第四站", 0);
 37         System.out.println(n);
 38     }
 39     
 40     /**
 41      * 计算站点间距离
 42      * @param station
 43      * @param start
 44      * @param end
 45      * @param stationNum 前面已经经过几站
 46      * @return
 47      */
 48     public static int getPosition(Station station,String start,String end,int stationNum){
 49         Iterator<Station> it=station.iterator();
 50         Station startStation=null;
 51         while(it.hasNext()){//找到起始站
 52             Station s=it.next();
 53             if(s.getStationName().equals(start)){
 54                 startStation=s;
 55                 break;
 56             }
 57         }
 58         Station next=startStation.getNextStation();
 59         int num=1;
 60         while(!next.getStationName().equals(end)){
 61             next=next.getNextStation();
 62             if(next==null){
 63                 return -1;//不可达
 64             }
 65             if(next.isTransfer){//如果是换成站
 66                 //循环所以可以换成的路线 递归调用getPosition
 67             }else{//如果不是换成站
 68                 
 69             }
 70             num++;
 71         }
 72         return stationNum+num;
 73     }
 74 
 75     /**
 76      * 站点类
 77      * @author Administrator
 78      *
 79      */
 80     static class Station implements Iterable<Station>{
 81         private int lineNum;//几号线
 82         private String stationName;//站名
 83         private boolean isStartOrEnd;//是否是首站或末站
 84         private boolean isTransfer;//是否是换乘站
 85         private Station nextStation;//該号线的下一站
 86         private Station preStation;//上一站
 87         private Set<Integer> transferStations;//可以换乘那几号线
 88         
 89         public Station(int lineNum,String stationName,boolean isStartOrEnd,boolean isTransfer){
 90             this.lineNum=lineNum;
 91             this.stationName=stationName;
 92             this.isStartOrEnd=isStartOrEnd;
 93             this.isTransfer=isTransfer;
 94         }
 95         
 96         public int getLineNum() {
 97             return lineNum;
 98         }
 99 
100         public void setLineNum(int lineNum) {
101             this.lineNum = lineNum;
102         }
103 
104         public String getStationName() {
105             return stationName;
106         }
107 
108         public void setStationName(String stationName) {
109             this.stationName = stationName;
110         }
111 
112         public boolean isStartOrEnd() {
113             return isStartOrEnd;
114         }
115 
116         public void setStartOrEnd(boolean isStartOrEnd) {
117             this.isStartOrEnd = isStartOrEnd;
118         }
119 
120         public boolean isTransfer() {
121             return isTransfer;
122         }
123 
124         public void setTransfer(boolean isTransfer) {
125             this.isTransfer = isTransfer;
126         }
127 
128         public Station getNextStation() {
129             return nextStation;
130         }
131 
132         public void setNextStation(Station nextStation) {
133             this.nextStation = nextStation;
134         }
135 
136         public Station getPreStation() {
137             return preStation;
138         }
139 
140         public void setPreStation(Station preStation) {
141             this.preStation = preStation;
142         }
143 
144         public Set<Integer> getTransferStations() {
145             return transferStations;
146         }
147 
148         public void addTransferStations(Integer transferStation) {
149             if(transferStations==null){
150                 transferStations=new HashSet<Integer>();
151             }
152             transferStations.add(transferStation);
153         }
154         
155 
156         @Override
157         public String toString() {
158             return "开始站---> [lineNum=" + lineNum + ", stationName="
159                     + stationName + ", isStartOrEnd=" + isStartOrEnd
160                     + ", isTransfer=" + isTransfer + "]";
161         }
162 
163         @Override
164         public Iterator<Station> iterator() {
165             return new  StationIterator(this);
166         }
167         
168     }
169     
170     static class StationIterator implements Iterator<Station>{
171         private Station station;
172         public StationIterator(Station station){
173             this.station=station;
174         }
175         @Override
176         public boolean hasNext() {
177             return station!=null;
178         }
179 
180         @Override
181         public Station next() {
182             Station temp=station;
183             this.station=temp.getNextStation();
184             return temp;
185         }
186 
187         @Override
188         public void remove() {
189             
190         }
191         
192     }
193 
194 }

 

 

 1 package org.train.test;
 2 import java.util. * ;
 3 public class Main {
 4 
 5     public static void main(String args[]) {
 6         Scanner cin = new Scanner(System. in );
 7         String station = "A1 A2 A3 A4 A5 A6 A7 A8 A9 T1 A10 A11 A12 A13 T2 A14 A15 A16 A17 A18 B1 B2 B3 B4 B5 T1 B6 B7 B8 B9 B10 T2 B11 B12 B13 B14 B15";
 8         String[] stations = station.split(" ");
 9         int length = stations.length;
10         int[][] arr = new int[length][length];
11         HashMap < String,
12         Integer > stationMap = new HashMap < String,
13         Integer > ();
14         for (int i = 0; i < stations.length; i++) {
15             stationMap.put(stations[i], i);
16         }
17         for (int i = 0; i < length; i++) {
18             for (int j = 0; j < length; j++) {
19                 if (i == j) {
20                     arr[i][j] = 0;
21                 } else {
22                     arr[i][j] = 1000;
23                 }
24             }
25         }
26         for (int i = 0; i < length - 1; i++) {
27             arr[i][i + 1] = 1;
28             arr[i + 1][i] = 1;
29         }
30         arr[9][25] = 0;
31         arr[14][31] = 0;
32         arr[25][9] = 0;
33         arr[31][14] = 0;
34         arr[stationMap.get("A18")][stationMap.get("B1")] = 1000;
35         arr[stationMap.get("B1")][stationMap.get("A18")] = 1000;
36 
37         arr[stationMap.get("A1").intValue()][stationMap.get("A18").intValue()] = 1;
38 
39         //Floyd算法求解
40         for (int k = 0; k < length; k++) {
41             for (int i = 0; i < length; i++) {
42                 for (int j = 0; j < length; j++) {
43                     if ((arr[i][k] + arr[k][j]) < arr[i][j]) {
44                         arr[i][j] = arr[i][k] + arr[k][j];
45                     }
46                 }
47             }
48         }
49 
50         while (cin.hasNext()) {
51             String state1 = cin.next();
52             String state2 = cin.next();
53 
54             int i = stationMap.get(state1);
55             int j = stationMap.get(state2);
56 
57             System.out.println(arr[i][j] + 1);
58         }
59     }
60 
61 }

 

地铁票价计算程序

标签:

原文地址:http://www.cnblogs.com/AllenIverson/p/4529088.html

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!