CCF CSP Brush Question Record 15-201609-2 Train Ticket Purchase (Java)

201609-2
Question name: Train ticket
time limit: 1.0s
Memory limit: 256.0MB
Problem Description:

Problem Description

  Please implement a simple seat allocation algorithm of a railway ticketing system to handle the seat allocation of a carriage.
  Suppose a car has 20 rows with 5 seats in each row. For convenience, we use 1 to 100 to number all the seats, the first row is 1 to 5, the second row is 6 to 10, and so on, the 20th row is 96 to 100.
  When buying tickets, one person may buy one or more tickets, up to 5 tickets. 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, it should be arranged in the few empty seats with the smallest number (regardless of whether they are adjacent or not).
  Assuming that all tickets were not purchased at the beginning, now some ticket purchase instructions are given, please handle these instructions.

Input format

  The first line of input contains an integer n , which represents 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 votes to be purchased, separated by a space between two adjacent numbers.

Output format

  Output n lines, each line corresponds to the processing result of an 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) Purchase 2 tickets and get seats 1 and 2.
  2) Buy 5 tickets and get seats 6-10.
  3) Purchase 4 tickets and get seats 11-14.
  4) Purchase 2 tickets and get seats 3 and 4.

Evaluation use case scale and conventions

  For all evaluation use cases, 1 ≤  n  ≤ 100, and the sum of all tickets purchased does not exceed 100.

import java.util.Scanner;
public class 火车购票 {

	public static void main(String[] args) {
		Scanner sc =new Scanner(System.in);
		int n=sc.nextInt();
		int[] a=new int[n+1];
		for(int i=1;i<=n;i++){
			a[i]=sc.nextInt();
		}
		int[][] pos=new int[20][5];
		
		for(int i=1;i<=n;i++){
			int mark=-1;
			int count=0;
			for(int x=0;x<20;x++){		
				for(int y=0;y<5;y++){
					
						if(pos[x][y]==0&&a[i]<=5){
							count++;
							if(count==a[i]){
								mark=y+1-a[i];
								for(int r=0;r<a[i];r++){
									pos[x][mark+r]=1;
									System.out.print(5*x+mark+1+r+" ");
								}
								System.out.println();
								break;
								
							}
							
						}
						
					}
				count=0;
				if(mark!=-1){
					
					break;
				}else{
					count=0;
					continue;
				}
			}
			if(mark==-1){
				 count=0;
				for(int k=0;k<20;k++){
					for(int j=0;j<5;j++){
						
						if(pos[k][j]==0){
								count++;
								pos[k][j]=1;
								System.out.print(k*5+(j+1)+" ");
							if(count==a[i]){
							System.out.println();
							break;
							}
						}
					}
					if(count==a[i]){
						break;
					}else{
						continue;
					}
				}
			}
			
		}
		
		
		
		
		

	}

}

 

Guess you like

Origin blog.csdn.net/m0_37483148/article/details/108324067
Recommended