Analysis of 12306 ticketing algorithm (Java version) _java

Source: Internet
Author: User

1. Take the G71 train as an example, first to the train station for placeholder code (from 1 to the last station accumulator)


A brief description of the above: G71 a total of 18 sites then our single seat ID can be represented by a 18-bit length binary string 10000000000000000 each represents a site, which is initialized to the following booking form every day before the ticket is released. The following numbers determine the maximum number of remaining votes according to the number of 0 in the seat ID


The originating restricted site in the booking table and the end to the restricted site can be flexibly matched (this can be implemented to limit the sale of the site)

2. Check the remaining votes

If we want to inquire about the date of 2016-06-11, predicate Baoding East Station (3) to Shaoguan Station (15), G71 second-class F seat ticket situation only need to execute the following SQL (this SQL can achieve the choice of seats and compartment, etc.)

Select GUID, train number, train type, seating type, compartment numbers, seat code, seat location from booking form

where To_number (substring (seat identification, 3,15)) =0

and departure date = ' 2016-06-11 '

and train code = ' G71 '

and substring (originating restricted station, 3,4) =1

and substring (end to restricted station, 15,16) =1

and Ticket status = ' For sale '

and train type = ' Second class '

and seating position = ' F '

3. Booking Tickets

3.1 Get a record according to the query condition in the second step and change the ticket status to lock

3.2 After the success of the lock to pay

3.2 After the successful payment will be Baoding to Shaoguan's ticket (000111111111111000 here predicate marked as 0) with the original ticket or operation, and the ticket status to be sold

100000000000000000 | 000111111111111000 = 100111111111111000 At this time the remainder of the ticket identification is the dynamic remaining ticket

3.3 If the specified time is not paid, then you can return the ticket status of this record to be sold

100111111111111000^000111111111111000 = 100000000000000000 This time the rest of the ticket and automatic restore back to the

4. Refund

To obtain the train to Shaoguan, the ticket (000111111111111000) and the corresponding ticket for the operation, you can return to the ticket pool

The following are related Java code

Import Java.math.BigDecimal;
public class Maintest {public static void main (string[] args) {String ticketflag = "";
int beginstation =;
int endstation =;
Long begintime = System.currenttimemillis ();
String result = Orderticket (Ticketflag, beginstation, endstation); if (Result.equals (Ticketflag)) {System.out.println ("Ticket Booking Failed")} else {System.out.println ("results after booking:" + result);//If you want to cancel,
You do this operation. String B = Buildticket (Ticketflag.length (), beginstation, endstation);
System.out.println ("released Result:" + Releaseticket (Ticketflag, b));
Long endtime = System.currenttimemillis ();
SYSTEM.OUT.PRINTLN ("Time Consuming:" + (Endtime-begintime)); /** * Booking * @param ticketflag * @param beginstation * @param endstation * @return/private static String Orderticket (String ticketflag, int beginstation, int endstation)
{String result = ' ";
if (Checkcanticket (Ticketflag, Beginstation, endstation)) {String b = buildticket (Ticketflag.length (), Beginstation,
Endstation); String currentticked = Toticket (ticketflag, b);
System.out.println ("Pre-vote results:" + ticketflag);
result = currentticked;
else {result = Ticketflag;};
return result;  /** * Cancellation of the booking * * @param ticketflag * @param b * @return/private static string Releaseticket (string Ticketflag, String
b) {StringBuilder tempst = new StringBuilder ("");
int length = Ticketflag.length (); for (int i =; i < length; i++) {Char Tempa = Ticketflag.charat (i); char TEMPB = B.charat (i); if (Tempa = = ' &&am P TEMPB = = ') {tempst.append ("");}
else {tempst.append (Tempa);}}
return tempst.tostring (); /** * Create interval placeholder * * @param length * @param beginstation * @param endstation * @return/private static String Buildticke
T (int length, int beginstation, int endstation) {StringBuilder st = new StringBuilder (""); for (int i =; i < length; i++) {if (I >= beginstation && i < endstation) {st.append ("");}
else {st.append ("");}}
System.out.println ("Create Interval ticket:" + st.tostring ());
return st.tostring (); /** * Generate the result of booking * * @param tIcketflag * @param b * @return/private static string Toticket (string ticketflag, String b) {StringBuilder tempst = new
StringBuilder ("");
int length = Ticketflag.length ();  for (int i =; i < length; i++) {Char Tempa = Ticketflag.charat (i); char TEMPB = B.charat (i); if (Tempa = = ' | | tempb = = = ') {tempst.append ("");}
else {tempst.append (Tempa);}}
return tempst.tostring (); /** * Can be booked * * @param ticketflag * @param beginstation * @param endstation * @return/private static Boolean CHECKC
Anticket (String ticketflag, int beginstation, int endstation) {Boolean result = false;
String Tempticket = ticketflag.substring (beginstation, endstation);
BigDecimal B = new BigDecimal (tempticket);
if (B.equals new BigDecimal (")) {result = true;}
return result;  }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.