CCF_Java_201609-2_ maximum fluctuation


Time limit: 1.0s
Memory Limit: 256.0MB

Problem Description

  • Implement a simple seat rail ticketing system allocation algorithm to deal with a seat assignment compartment.
  • Suppose a carriage 20 rows, each five seats. For convenience, we use the 1-100 number to give all the seats, the first row is number 51, the second row is the number 106, and so, the first row 20 is 96 to 100.
  • When booking, a person may purchase one or more tickets, up to no more than five. If this many tickets can be arranged in the same row numbers adjacent seat, it should be on the smallest number of adjacent seats. Otherwise it should be arranged in the smallest number in a few empty seats (without regard to whether neighboring).
  • Assuming that the initial ticket purchase is not all, now give some instruction tickets, you deal with these instructions.

Input Format

  • Comprising a first line of input integer n, the number of instructions tickets.
  • The second line contains n integers, each integer p between 1 and 5, showing the number of votes to be purchased, using a space separation between two adjacent numbers.

Output Format

  • N output lines, each line corresponding to the processing result of an instruction.
  • For tickets instruction p, p output a ticket number, in ascending order.

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. 2 purchase tickets, to give 1,2 seat.
  2. 5 purchase tickets, to give the seat 6 to 10.
  3. 4 purchase tickets, to give the seat 11 to 14.
  4. 2 purchase tickets, to give 3,4 seat.

Evaluation scale cases and agreed with

  • For the evaluation all use cases, 1 ≤ n ≤ 100, all number of tickets and no more than 100.

Code

import java.util.Scanner;

public class Main {
	
	static boolean[][] seat=new boolean[20][5];
	
	public static void main(String[] args) {
		int n,num;
		
		Scanner sc=new Scanner(System.in);
		
		n=sc.nextInt();
		for(int i=0;i<n;i++) {
			num=sc.nextInt();
			get(num);//出票
			System.out.println();
		}
		sc.close();		
	}
		
	static void get(int num) {
		int temp=0;
		for(int i=0;i<20;i++) {
			for(int j=0;j<5;j++) {
				if(!seat[i][j]){
					temp++;
				}else {
					temp=0;//有一个间断的就重新开始计算
				}				
				if(temp==num) {
					for(int k=j+1-num;k<=j;k++) {
						seat[i][k]=true;//标记出票
						System.out.print(5*i+k+1+" ");//输出结果
					}
					return;
				}
			}
			temp=0;//一排没有满足条件的也清零
		}//如果有连续的座位则直接出连续的票数,并且直接结束方法
		
		for(int i=0;i<20;i++) {
			for(int j=0;j<5;j++) {
				if(!seat[i][j]) {
					seat[i][j]=true;//对出票的座位做标记
					System.out.print(5*i+j+1+" ");
				}
			}
		}//没有连续的票坐则使用单独的票,并且按照大小出票		
	}
}
Published 47 original articles · won praise 2 · Views 2219

Guess you like

Origin blog.csdn.net/weixin_43662429/article/details/104829024