CCF CSP 201609-2 train tickets (Java-100 points)


Question number: 201609-2
Questions Name: Train tickets
time limit: 1.0s
Memory Limit: 256.0MB
Problem Description:
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.


Problem Description : Implement a simple railway ticketing system of seat allocation algorithm to deal with a seat assignment compartment.

Analysis : The problem there is a trap, is easy to forget to consider when even the number of tickets have been sold in the same row, row tickets sold unusual treatment. Ignore this scenario, the program can only get 90 points. Program uses the array of each row represents a number of votes has been sold, this idea is given to facilitate the determination condition in both cases.

After submitting 100 points in the Java language program is as follows :( remove all comments commit, otherwise easily cause a compiler error)

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		int M = 20;
		int a[] = new int[M];

		int n, p;
		Scanner sc = new Scanner(System.in);
		n = Integer.valueOf(sc.nextLine());

		while (n-- != 0) {
			p = sc.nextInt();
			boolean flag = false;
			// 首先判断是否能够买到同一排相邻的座位
			for (int i = 0; i < 20; i++) {
				if (a[i] + p <= 5) {// a[i]表示第i组的5个位置已售出票数
					for (int j = 0; j < p; j++) {
						if (j == p - 1)
							System.out.print(i * 5 + a[i] + j + 1);
						else
							System.out.print(i * 5 + a[i] + j + 1 + " ");
					}
					a[i] += p;
					flag = true;
					break;
				}
			}
			// 如果不能买到同一排相邻的座位,就依次寻找空的座位
			if (!flag) {
				int num = 0;
				for (int i = 0; i < 20; i++) {
					if (a[i] < 5) {
						num++;
						if (num == p) {
							System.out.print(i * 5 + a[i] + 1);
							break;
						} else
							System.out.print(i * 5 + a[i] + 1 + " ");
						a[i]++;
						i--;// 因为在这一排还可能有空座位剩余,故回退,当a[i]=5则不用回退
					}
				}
			}
			System.out.println();
		}

		sc.close();
	}
}




Published 128 original articles · won praise 238 · Views 460,000 +

Guess you like

Origin blog.csdn.net/daijin888888/article/details/76572601