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. Input format The first line of the input contains an integer n, indicating the number of ticket purchase instructions. Output format Output n lines, each line corresponds to the processing result of an instruction. sample input 4 sample output 1 2 Sample explanation 1) buy 2 tickets and get seats 1 and 2. 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; }