可以直接運用java API來完成這道題,不用自己去實現一個隊列了。
import java.util.LinkedList;import java.util.Queue;import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Queue<Integer> queue = new LinkedList<>(); for (int i = 0; i < n; i++) { int op = sc.nextInt(); switch (op) { case 1: queue.add(sc.nextInt()); break; case 2: if (queue.isEmpty()) { System.out.PRintln("no"); return ; } else { System.out.println(queue.poll()); } break; case 3: System.out.println(queue.size()); break; default: break; } } sc.close(); }}
新聞熱點
疑難解答