본문 바로가기
알고리즘 문제 (백준저지)/기타

[백준/10845] 큐 (Java/코드)

by 유헤 2019. 3. 3.

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


시간 제한메모리 제한제출정답맞은 사람정답 비율
1 초256 MB2255111095880150.829%

문제

정수를 저장하는 큐를 구현한 다음, 입력으로 주어지는 명령을 처리하는 프로그램을 작성하시오.

명령은 총 여섯 가지이다.

  • push X: 정수 X를 큐에 넣는 연산이다.
  • pop: 큐에서 가장 앞에 있는 정수를 빼고, 그 수를 출력한다. 만약 큐에 들어있는 정수가 없는 경우에는 -1을 출력한다.
  • size: 큐에 들어있는 정수의 개수를 출력한다.
  • empty: 큐가 비어있으면 1, 아니면 0을 출력한다.
  • front: 큐의 가장 앞에 있는 정수를 출력한다. 만약 큐에 들어있는 정수가 없는 경우에는 -1을 출력한다.
  • back: 큐의 가장 뒤에 있는 정수를 출력한다. 만약 큐에 들어있는 정수가 없는 경우에는 -1을 출력한다.

입력

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지 않은 명령이 주어지는 경우는 없다.

출력

출력해야하는 명령이 주어질 때마다, 한 줄에 하나씩 출력한다.

예제 입력 1 

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

예제 출력 1 

1
2
2
0
1
2
-1
0
1
-1
0
3




처음에 개발했을땐, Queue에 대해서는 이해하고 있었는데.. 

너무 불편하게 코드를 짰다, 삼항연산자와 isEmpty 를 이용해서 코드를 더욱더 간단하게 활용할 수 있다.


queue에 관한 내용은 https://skyvvv624.blog.me/220950557929 에 적혀져 있다. 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
public class bj_10845 {
    
    public static void main(String[] args) throws Exception {
        
        long start = System.currentTimeMillis(); 
 
        Queue<Integer> q = new LinkedList<Integer>();
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        
        while( n-- > 0 ) {
            String str = sc.next();
            switch(str) {
            
            case "push":
                int pushN = sc.nextInt();
                q.offer(pushN);
                break;
            case "front":
                if( q.peek() != null ) {
                    int frontN = q.peek();
                    System.out.println(frontN);
                } else {
                    System.out.println("-1");
                }        
                break;
            case "back":
                int sz = q.size();
                forint i =0 ; i<sz ; i++) {
                    if( q.peek() != null ) {
                        if( sz-1 == i ) {                        
                            System.out.println(q.element());
                            q.add(q.poll());
                        }
                        else {
                            q.add(q.poll());
                        }
                    }
                }
                
                break;
            case "empty":
                if( q.isEmpty() )
                    System.out.println("1");
                else
                    System.out.println("0");
                break;
            case "pop":
                if( q.peek() != null ) {
                    int popN = q.poll();
                    System.out.println(popN);
                } else {
                    System.out.println("-1");
                }        
                
                break;
            case "size":
                System.out.println(q.size());
                break;
                
                
            
            }
        }
        
        long end = System.currentTimeMillis(); //프로그램이 끝나는 시점 계산
        System.out.println"실행 시간 : " + ( end - start )/1000.0 +"초");
 
    }
 
}
 
cs


아래가 성공한 코드이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class bj_10845 {
    
    public static void main(String[] args) throws Exception {
        
        long start = System.currentTimeMillis(); 
        Queue<Integer> q = new LinkedList<Integer>();
        Scanner sc = new Scanner(System.in);        
        int n = sc.nextInt();
        int pushN = 0;
        while( n-- > 0 ) {
            String str = sc.next();
            
            switch(str) {
            case "push":
                pushN = sc.nextInt();
                q.add(pushN);
                break;
            case "front":
                System.out.println( q.isEmpty()?-1:q.peek() );    
                break;
            case "back":
                System.out.println( q.isEmpty()?-1:pushN);
                break;
            case "empty":
                System.out.println( q.isEmpty()?1:0);
                break;
            case "pop":
                System.out.println( q.isEmpty()?-1:q.poll());
                break;
            case "size":
                System.out.println( q.size());
                break;
 
            }
        }
        
        long end = System.currentTimeMillis(); //프로그램이 끝나는 시점 계산
        System.out.println"실행 시간 : " + ( end - start )/1000.0 +"초");    
    }
}
 
cs