Algorithm note _080: Blue Bridge cup practice queue operation (Java)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.