CSP201609-2 Train ticket purchase (Python)

Article directory

topic

Question No.: 201609-2
Question name: train ticket
time limit: 1.0s
Memory limit: 256.0MB

Problem description
  Please implement a simple seat allocation algorithm of a railway ticketing system to handle the seat allocation of a carriage.
  Assume that a carriage has 20 rows with 5 seats in each row. For convenience, we use 1 to 100 to number all seats, the first row is 1 to 5, the second row is 6 to 10, and so on, and the 20th row is 96 to 100.
  When purchasing tickets, a person may purchase one or more tickets, up to five. If these tickets can be arranged in adjacent seats in the same row, they should be arranged in the adjacent seats with the lowest number. Otherwise, they should be arranged in the few empty seats with the lowest number (regardless of whether they are adjacent).
  Assuming that none of the tickets were purchased at the beginning, and now some ticket purchase instructions are given, please process these instructions.

Input format
  The first line of input contains an integer n, indicating the number of ticket purchase instructions.
  The second line contains n integers, each integer p is between 1 and 5, indicating the number of tickets to be purchased, and a space is used to separate two adjacent numbers.

Output format
  Output n lines, each line corresponds to the processing result of a command.
  For the ticket purchase instruction p, output the numbers of p tickets, sorted from small to large.

Sample input
  4
  2 5 4 2

Sample output
  1 2
  6 7 8 9 10
  11 12 13 14
  3 4

Example description
  1) Buy 2 tickets and get seats 1 and 2.
  2) Buy 5 tickets and get seats 6 to 10.
  3) Buy 4 tickets and get seats 11 to 14.
  4) Buy 2 tickets and get seats 3 and 4.

Evaluation use case scale and agreement
  For all evaluation use cases, 1 ≤ n ≤ 100, the sum of all purchased tickets shall not exceed 100.

code

# 输入购票指令次数
n = int(input())

# 初始化火车座位状态列表
seat = [0 for i in range(100)]

# 记录每行座位空位数量列表
row = [5 for i in range(20)]

# 输入购买指令
ticket = list(map(int,input().split()))

# 循环输入
for x in ticket:
    # 选到连续座位标记
    complete = False
    # 遍历每行(空位数量)
    for j in range(20):
        # 当此行空位足够
        if row[j] >= x:
            count = 0 # 已选计数器
            # 选择x个作座位并输出
            for k in range(5):
                if seat[j*5+k] == 0:
                    seat[j*5+k] = 1
                    count += 1
                    print(j*5+k+1,end=" ")
                if count == x:
                    print()
                    break
            # 更新当前排的座位数
            row[j] -= x
            complete = True
            break

    # 当车厢内不存在连续的座位
    count = 0 # 已选座位计数器
    i = 0 # 座位序号
    # 选择x个作座位并输出
    while complete == False:
        # 当此时为空座 选中该座位输出 更新当前排的座位数
        if seat[i] == 0:
            seat[i] = 1
            row[int(i/5)] -= 1
            count += 1
            print(i+1,end=" ")
        if count == x:
            print()
            complete = True
        i += 1

Guess you like

Origin blog.csdn.net/qq_45899597/article/details/113819339