nohriter   1년 전

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class Main {

    public static void main(String args[]) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.<i>in</i>));
        Queue<Character> Q = new LinkedList<>();
        StringBuilder sb = new StringBuilder();

        int N = Integer.<i>parseInt</i>(br.readLine());
        char last = 0;

        for (int i = 0; i < N; i++) {
            String s = br.readLine();
            if (s.startsWith("push")) {
                Q.offer(s.charAt(5));
                last = s.charAt(5);
            } else if (s.equals("pop")) {
                if (!Q.isEmpty()) sb.append(Q.poll()).append("\n");
                else sb.append("-1\n");
            } else if (s.equals("size")) {
                sb.append(Q.size() + "\n");
            } else if (s.equals("empty")) {
                if (Q.isEmpty()) sb.append("1\n");
                else sb.append("0\n");
            } else if (s.equals("front")) {
                if (Q.isEmpty()) sb.append("-1\n");
                else sb.append(Q.peek() + "\n");
            } else if (s.equals("back")) {
                if (Q.isEmpty()) sb.append("-1\n");
                else sb.append(last + "\n");
            }
        }
        System.<i>out</i>.println(sb);
    }

}

15
push 1
push 2
front
back
size
empty
pop
pop
pop
size
empty
pop
push 3
empty
front
1
2
2
0
1
2
-1
0
1
-1
0
3

stylecoke   1년 전

반례입니다.

nohriter   1년 전

// stylecoke

헉.. 정말 감사합니다! 너무 답답했는데 ㅠㅠ

알려주시니 보이네요!! 큰 가르침 얻어갑니다 행복하세요!!

댓글을 작성하려면 로그인해야 합니다.