Analysis of 12306 ticketing algorithm (Java version)

1. Take G71 train as an example, first carry out occupation code for train number platform (add it from 1 to the last station)

A brief description of the above occupation is as follows: G71 has a total of 18 stations, then the seat identification of our single seat can be represented by an 18 bit binary string, 1000000000000000000. Each bit represents a station and is initialized to the following booking table before ticket release every day. The data is as follows. The maximum number of remaining tickets is determined according to the number of zeros in the seat identification

The originating restricted site and the ending restricted site in the ticket booking table can be flexibly matched (this can realize the sale of restricted sites)

2. Query remaining tickets

If we want to query the remaining tickets of seat f of G71 second-class seat from Baoding east station (3) to Shaoguan station (15) on June 11, 2016, we only need to execute the following SQL (this SQL can realize the functions of seat selection and carriage selection)

Select guid, train number code, train number type, seat type, car number, seat code, seat position from booking table

where to_ Number (substring, 3,15)) = 0

And departure date = '2016-06-11'

And train number code = 'G71'

And substring (departure restricted station, 4) = 1

And substring (terminal restricted station, 15,16) = 1

And ticket status = 'for sale'

And train number type = 'second class seat'

And seat position = 'f'

3. Booking tickets

3.1 obtain a record according to the query conditions in step 2, and then change the ticket status to locked

3.2 payment will be made after successful locking

3.2 after successful payment, the ticket from Baoding to Shaoguan (000111111111111000, the origin station here is marked as 0) is or calculated with the original ticket, and the ticket status is changed to for sale

10000000000000000 | 000111111111000 = 100111111000. The remaining ticket ID at this time is the dynamic remaining ticket

3.3 if the specified time is not paid, the ticket status of this record can be restored to for sale

100111111000 ^ 000111111111111000 = 1000000000000000. The remaining tickets at this time are automatically restored

4. Refund

Get the ticket (000111111111000) from Baoding to Shaoguan of this train number and perform non calculation with the corresponding ticket, then you can return to the ticket pool

The following is the relevant java code

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>