周五閒來無事,基於上一篇關於初探12306售票算法(一)-理論,進行了java編碼實踐供各位讀者參考(以下為相關代碼的簡單描述)
1.1初始化一列車廂的票據信息
/** * 生成Ticket信息 * * @param train * @return */ public static List<Ticket> initTicketList(Train train) { List<Ticket> result = new ArrayList<Ticket>(); Map<String, Integer> seatMap = train.getSeatTotalNum(); for (Entry<String, Integer> entry : seatMap.entrySet()) { int ticketSize = entry.getValue(); String ticketType = entry.getKey(); for (int i = 0; i < ticketSize; i++) { int saleChannel = (int) (Math.random() * 10) % 8; Ticket ticket = new Ticket(); ticket.setSaleChannel(saleChannel); ticket.setTicketType(ticketType); ticket.setGuid(UUID.randomUUID().toString()); ticket.setFromDate(train.getFromDate()); ticket.setTicketFlag(CommonUtil.initTicketFlag(train)); ticket.setTrainNo(train.getTrainNo()); result.add(ticket); } } return result; }
1.2 生成站點購票(比如說第進行移位即可如第1站1,第二站‘10’ 這裡返回的十進制的)
/** * 創建 * @param i * @param stationNum * @return */ public static String buidTicket(int i, int stationNum) { BigInteger temp = new BigInteger("0"); for (int j = i; j < stationNum; j++) { temp = temp.or(new BigInteger(buidTicket(j))); } return temp.shiftRight(1).toString(); }
1.3 訂票主程序,這裡一次只定一張票(A=A|B)
/** * 根據篩選條件取得對應的車次 * @param ticketStr * @param ticketList * @param condition * @return */ public static Order createOrderByCondition(String ticketStr,List<Ticket> ticketList,Map condition){ Order tempOrder = null; for (Ticket ticket : ticketList) { BigInteger toTicket = new BigInteger(ticketStr); BigInteger fromTicket = new BigInteger(ticket.getTicketFlag()); // 如果可以訂票,那麼久進行扣除庫存&& // (ticket.getSaleChannel()==(ticket.getSaleChannel()|1)) if (canTicket(fromTicket, toTicket) &&ticket.getTicketType().equals(condition.get("ticketType").toString()) //&&(ticket.getSaleChannel()==(ticket.getSaleChannel()|2)) ) { tempOrder = new Order(); tempOrder.setOrderId(UUID.randomUUID().toString()); tempOrder.setSeatType(ticket.getTicketType()); tempOrder.setTicketFlag(toTicket.toString()); tempOrder.setTrainNO(ticket.getTrainNo()); tempOrder.setFromDate(ticket.getFromDate()); tempOrder.setSaleChannel(ticket.getSaleChannel()); tempOrder.setTicketGuid(ticket.getGuid()); ticket.setTicketFlag(fromTicket.or(toTicket).toString()); break; } } return tempOrder; }
1.4 判斷是否郵票,A=~(~A|B)
/** * 訂票判斷是否可以訂票 * * @param fromTicket * @param toTicket * @return */ private static boolean canTicket(BigInteger fromTicket, BigInteger toTicket) { return fromTicket.equals(fromTicket.not().or(toTicket).not()); }
5.1初始化一列車廂
5.2按照逐站購票的形式,最大化 即 AB BC CD DE EF等等
5.3輸出耗時時間
1 package com.train.main; 2 3 import java.io.File; 4 import java.math.BigDecimal; 5 import java.math.BigInteger; 6 import java.util.ArrayList; 7 import java.util.HashMap; 8 import java.util.List; 9 import java.util.Map; 10 11 import com.train.ticket.Order; 12 import com.train.ticket.Ticket; 13 import com.train.ticket.Train; 14 import com.train.util.CommonUtil; 15 16 public class MainTest { 17 18 public static void main(String[] args) { 19 Train train = new Train(); 20 train.setTrainNo("0909123SA873878"); 21 train.setFromDate("2016/06/11"); 22 train.setStationNum(18); 23 Map<String, Integer> seatMap = new HashMap<String, Integer>(); 24 seatMap.put("商務座", 500); 25 seatMap.put("一等座", 1000); 26 seatMap.put("二等座", 2000); 27 train.setSeatTotalNum(seatMap); 28 // 生成票據 29 System.out.println("初始化列車中的票"); 30 List<Ticket> ticketList = CommonUtil.initTicketList(train); 31 String fileName = "D:\\RESULT.txt"; 32 File f = new File(fileName); 33 if(f.exists()) f.delete(); 34 long startTime = System.currentTimeMillis(); 35 //int i = 0; 36 //for (Ticket ticket : ticketList) { 37 // CommonUtil.appendMethodA(fileName, 38 // i + "||" + CommonUtil.toJSON(ticket) + "\n", true); 39 // i++; 40 //} 41 System.out.println("開始訂票"); 42 long beginTime = System.currentTimeMillis(); 43 List<Order> orderResult = new ArrayList<Order>(); 44 45 for (int j = 0; j < train.getStationNum() - 1; j++) { 46 String ticketStr = CommonUtil.buidTicket(j); 47 //String ticketStr = CommonUtil.buidTicket(1,train.getStationNum());; 48 //System.exit(0); 49 50 List<Order> tempListOrder = CommonUtil.createOrderList(ticketStr, 51 ticketList, train); 52 orderResult.addAll(tempListOrder); 53 54 } 55 long endTime = System.currentTimeMillis(); 56 System.out.println("訂票完成"); 57 //int m = 0; 58 59 // for (Ticket ticket : ticketList) { 60 // String temp = m + "||" + CommonUtil.toJSON(ticket) + ","; 61 // // System.out.println(temp); 62 // CommonUtil.appendMethodA(fileName, temp, true); 63 // m++; 64 // } 65 // int k = 1; 66 // for (Order order : orderResult) { 67 // String temp = order.getOrderId() 68 // + "||" + order.getSaleChannel() 69 // + "||" + order.getFromDate() 70 // + "||" + order.getSeatType() 71 // + "||" + order.getTicketGuid() 72 // + "||" + order.getTrainNO() 73 // + "||" + order.getTicketFlag() 74 // + "||" + new BigInteger(order.getTicketFlag()).toString(2) 75 // + "||" +k; 76 // CommonUtil.appendMethodA(fileName,temp, true); 77 // k++; 78 // } 79 80 long eedTime = System.currentTimeMillis(); 81 System.out.println("生成訂單" + orderResult.size() + "||耗時時間:" 82 + (endTime - beginTime) + "毫秒"); 83 System.out.println("每秒鐘生成單據數(逐張訂票)"+ new BigDecimal(orderResult.size()).multiply(new BigDecimal(1000)).divide(new BigDecimal(endTime - beginTime),0,BigDecimal.ROUND_HALF_DOWN)); 84 85 System.out.println("執行完畢"); 86 87 } 88 89 } View Code6.運行結果
完整代碼: 源碼
本文原創:轉載請注明出處 http://www.cnblogs.com/feichengwurao/p/5202100.html