#53825: java


s310186@student.cysh.cy.edu.tw (陳躍文)

學校 : 不指定學校
編號 : 280106
來源 : [163.27.3.94]
最後登入時間 :
2025-10-01 10:50:29

詳細影片?

 

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Scanner;

public class VectorQueueToQueue {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        Queue<Integer> queue = new ArrayDeque<>();
        
        int n;
        
        while (scanner.hasNextInt()) {
            n = scanner.nextInt();
            
            int i = 0;
            while (i < n) {
                if (!scanner.hasNextInt()) break;
                int input = scanner.nextInt();
                
                switch (input) {
                    case 1:
                        if (!scanner.hasNextInt()) break;
                        int x = scanner.nextInt();
                        queue.offer(x);
                        break;

                    case 2:
                        if (queue.isEmpty()) {
                            System.out.println(-1);
                        } else {
                            System.out.println(queue.peek());
                        }
                        break;

                    case 3:
                        if (!queue.isEmpty()) {
                            queue.poll();
                        }
                        break;
                }
                i++;
            }
        }
        
        scanner.close();
    }
}