CCF-CSP 201609-2 Train ticket purchase

Problem description
  Please implement a simple seat allocation algorithm for a railway ticket purchase system to handle the seat allocation of a car.
  Suppose a car has 20 rows with 5 seats in each row. For convenience, we use 1 to 100 to number all seats. The first row is number 1 to 5, the second row is number 6 to 10, and so on. The 20th row is number 96 to 100.
  When buying tickets, a person may buy one or more tickets, no more than 5 tickets. If these tickets can be arranged in the same row of adjacent seats, they should be arranged in the adjacent seats with the lowest number. Otherwise, it should be arranged in the few empty seats with the lowest number (regardless of whether they are adjacent).
  Assuming that all the tickets were not purchased at the beginning, some ticket purchase instructions are now given. Please handle 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, and each integer p is between 1 and 5, indicating the number of tickets to be purchased, separated by a space between two adjacent numbers.
Output format
  Output n lines, each line corresponds to the processing result of one instruction.
  For the ticket purchase instruction p, output the number 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
Sample description
  1) Buy 2 tickets and get seats 1 and 2.
  2) Purchase 5 tickets and get seats 6 to 10.
  3) Purchase 4 tickets and get seats 11 to 14.
  4) Buy 2 tickets and get seats 3 and 4.
Evaluation use case size and agreement
  For all evaluation use cases, 1 ≤ n ≤ 100, the sum of all ticket purchases does not exceed 100.

Summary of experience:
define a two-dimensional array of seats, the first column of each row is defined as the number of empty seats in the row, initialized to 5.
The second to sixth columns of each row are defined as seats, initialized to 0, representing empty seats.
Hard core simulation is sufficient.

C ++ code:

#include<bits/stdc++.h>
using namespace std;
int sit[20][6];
int main() {
	int n;
	scanf("%d",&n);
	for(int i=0; i<20; i++) { //初始化每排座位的空座位数为5
		sit[i][0] = 5;
	}
	while(n--) {
		int p;
		bool flag = false;
		scanf("%d",&p);
		for(int i=0; i<20; i++) { //首先寻找是否有一排满足连续的座位
			if(sit[i][0]>=p) { //该排座位满足连续座位
				sit[i][0] -= p; //该排空座位数减去购买票数
				for(int j=1; j<6&&p>0; j++) { //遍历座位,寻找空座位
					if(0 == sit[i][j]){
						printf("%d ",5*i+j); //输出空座位
						p--;
						sit[i][j] = 1; //该座位坐人了
					}
				}
				flag = true;
				break;
			}
		}
		if(!flag){ //若没有一排是满足连续的座位,则一个一个寻找空位(可以不连续)
			for(int i=0;i<20&&p>0;i++){
				for(int j=1;j<6&&p>0;j++){
					if(0 == sit[i][j]){
						printf("%d ",5*i+j);
						p--;
						sit[i][0]--;
						sit[i][j] = 1;
					}
				}
			}
		}
		printf("\n");
	}
	return 0;
}
Published 111 original articles · won praise 2 · Views 3533

Guess you like

Origin blog.csdn.net/m0_38088647/article/details/100665990