CCF · 201609-2 · train ticket (java&c + +)

Keywords: Java

Test question No.: 201609-2
Test question Name: Train ticketing
time limit: 1.0s
Memory limit: 256.0MB
Problem Description:

Problem description

Please implement a simple seat allocation algorithm of railway ticketing system to handle the seat allocation of a car.
Suppose a car has 20 rows, five 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. The 20th row is 96 to 100.
When buying tickets, one person may buy one or more tickets, no more than 5 at most. If these tickets can be arranged in adjacent seats of the same row, they should be arranged in the adjacent seats with the smallest number. Otherwise, it should be arranged in the few empty seats with the smallest number (regardless of whether they are adjacent or not).
Suppose that at the beginning all the tickets were not purchased, and now there are some instructions for buying tickets. Please handle these instructions.

Input format

The first line of the input contains an integer n, indicating the number of ticket purchase instructions.
The second line contains n integers, each of which p is between 1 and 5, indicating the number of votes to be purchased, and the adjacent two numbers are separated by a space.

Output format

Output n lines, each line corresponds to the processing result of an instruction.
For the ticket purchase instruction p, the number of p tickets is output and 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 explanation

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

Scale and convention of evaluation case

For all evaluation cases, 1 ≤ n ≤ 100, and the sum of all ticket purchases shall not exceed 100.

java:

import java.util.Scanner;
import java.util.LinkedList;

public class Main {

	public static void main(String[] args) {
		LinkedList[] queue = new LinkedList[20];
        for(int i=0;i<queue.length;i++){
            queue[i] = new LinkedList();
            for(int j=0;j<5;j++)
                queue[i].offer(i*5+j+1);
        }

        Scanner input = new Scanner(System.in);
        int qty = input.nextInt();
        for(int i=0;i<qty;i++){
            int num = input.nextInt();
            boolean flag = true;
            for(int j=0;j<queue.length;j++){
                if(queue[j].size()>=num){
                    for(int k=0;k<num;k++)
                        System.out.print
                        (queue[j].poll()+" ");
                    flag = false;
                    break;
                }
            }
            if(flag){
                int count = 0;
                for(int j=0;j<queue.length&&flag;j++){
                    while(!queue[j].isEmpty()){
                        System.out.print
                        (queue[j].poll()+" ");
                        count++;
                        if(count==num){
                            flag = false;
                            break;
                        }
                    }
                }
            }
            System.out.println();
        }
    }
}

c++:

#include <iostream>
#include <queue>

using namespace std;

queue<int> mp[21];
int N;
int main(void){
    for(int i=1,j=1;i<=20;i++)
	{ 
        mp[i].push(j++);
        mp[i].push(j++);
        mp[i].push(j++);
        mp[i].push(j++);
        mp[i].push(j++);
    }
    cin>>N;
    int num;
    for(int i=0;i<N;i++)
	{
        cin>>num;
        bool ok=false;
        for(int row=1;row<=20;row++)
		{
            if(mp[row].size()>=num)
			{
                for(int j=0;j<num;j++)
				{
                    cout<<mp[row].front()<<' ';
                    mp[row].pop();
                }
                ok=true;
                break;
            }
        }
        if(!ok)
		{
            for(int row=1;row<=20&&num>0;row++)
			{
                while(num>0&&!mp[row].empty())
				{
                    cout<<mp[row].front()<<' ';
                    mp[row].pop();
                    num--;
                }
            }
        }
        cout<<endl;
    }
    return 0;
}

 

Posted by Kairu on Tue, 31 Dec 2019 22:04:40 -0800