Directory
1 Problem Description
2 Solutions
1 problem description Problem DescriptionQueue operation problem. According to the input Operation command, the operation queue (1) is queued, (2) is out of line and outputs, (3) calculates the number of elements in the team and outputs. Input FormatThe first line is a number n.
The following n lines, the first number of each line for the operation command (1) queue, (2) out of the team and output, (3) Calculate the number of elements in the team and output. output Formatseveral rows each display the output of a 2 or 3 command. Note: 2. The queue may appear empty queue (underflow), please output "no", and exit. Sample Input7
1
1
2
3
2
3
2Sample Output +
1
About
0
Nodata size and conventions1<=n<=50 2 Solutions
Special attention: Note: 2. The queue may appear empty queue (underflow), please output "no", and exit .
The specific code is as follows:
Importjava.util.ArrayList;ImportJava.util.Scanner; Public classMain { Public voidPrintresult (int[] operation) {ArrayList<Integer> list =NewArraylist<integer>(); for(inti = 0;i < operation.length;i++) { if(Operation[i][0] = = 1) List.add (operation[i][1]); Else { if(Operation[i][0] = = 2) { if(list.size () = = 0) {System.out.println ("No"); return;//Note that at this point test instructions requires a direct exit } Else{System.out.println (List.get (0)); List.remove (0); } } Else if(Operation[i][0] = = 3) {System.out.println (List.size ()); } } } return; } Public Static voidMain (string[] args) {main test=NewMain (); Scanner in=NewScanner (system.in); intn =In.nextint (); if(N < 1 | | n > 50) return; int[] Operation =New intN [2]; for(inti = 0;i < n;i++) { intA =In.nextint (); if(A = = 1) { intb =In.nextint (); operation[i][0] =A; operation[i][1] =b; } Else{operation[i][0] =A; }} test.printresult (operation); }}
Algorithm note _080: Blue Bridge cup practice queue operation (Java)