본문으로 바로가기
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

백준알고리즘


https://www.acmicpc.net/problem/10845


Front만 유의하면 쉬운 문제

import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); int count = Integer.parseInt(scan.next().trim()); int lastValue = 0; Queue<Integer> que = new LinkedList<Integer>(); for(int i=0; i <= count; i++) { String command = scan.nextLine().trim(); if(command.contains("push")) { int data = Integer.parseInt(command.substring(5, command.length())); lastValue = data; que.offer(data); } else if(command.contains("pop")) { if(que.size() == 0) { System.out.println(-1); } else { System.out.println(que.poll()); } } else if(command.contains("front")) { if(que.size() == 0) { System.out.println(-1); } else { System.out.println(que.peek()); } } else if(command.contains("back")) { if(que.size() == 0) { System.out.println(-1); } else { System.out.println(lastValue); } } else if(command.contains("empty")) { if(que.size() == 0) { System.out.println(1); } else { System.out.println(0); } } else if(command.contains("size")) { System.out.println(que.size()); } } scan.close(); } }