ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 백준 10866 덱 | 자바 java | BufferedReader BufferedWriter
    [백준 알고리즘]/[자바 Java] 2024. 1. 18. 17:35
    728x90
    반응형

     

    이번 포스팅은 백준 10866번 덱입니다.

    아래 url를 클릭하시면 백준 사이트에서 문제를 볼 수 있습니다.

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

     

    10866번: 덱

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

    www.acmicpc.net

     

     

     

    기본 알고리즘

    • 구현
    • 자료구조

     

    반응형

     

    전체 코드

     

    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
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    import java.io.*;
    import java.util.StringTokenizer;
     
     
    public class Main{
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
            String input="", output="";
            StringTokenizer st = new StringTokenizer(input);
            int n=0;
     
            input = br.readLine();
            st = new StringTokenizer(input);
            n = Integer.parseInt(st.nextToken());
            
            int[] deque = new int[n];
            int size = 0;
        
     
            for(int i=0; i<n; i++){
                input = br.readLine();
                st = new StringTokenizer(input);
                output="";
     
                switch (st.nextToken()) {
                    case "push_front":
                        int[] deque2 = new int[size];
                        for(int j=0; j<size; j++){
                            deque2[j]=deque[j];
                        }
     
                        deque[0]=Integer.parseInt(st.nextToken());
     
                        int idx=0;
                        for(int k=1; k<size+1; k++){
                            deque[k] = deque2[idx++];
                        }
                        size++;
     
                        break;
     
                    case "push_back":
                        deque[size]=Integer.parseInt(st.nextToken());
                        size++;
     
                        break;
     
                    case "pop_front":
                        if(size==0){
                            output = String.valueOf(-1);
                        } else {
                            output = String.valueOf(deque[0]);
     
                            int[] deque3 = new int[size-1];
                            int size3=0;
                            for(int j=1; j<size; j++){
                                deque3[size3]=deque[j];
                                size3++;
                            }
     
                            for(int k=0; k<size-1; k++){
                                deque[k] = deque3[k];
                            }
     
                            size--;
                        }
                        
                        break;
     
                    case "pop_back":
                        if(size==0){
                            output = String.valueOf(-1);
                        } else {
                            output = String.valueOf(deque[size-1]);
                            size--;
                        }
                        
                        break;
     
                    case "size":
                        output = String.valueOf(size);
                        
                        break;
     
                    case "empty":
                        if(size==0){
                            output = String.valueOf(1);
                        } else {
                            output = String.valueOf(0);
                        }
     
                        break;
     
                    case "front":
                        if(size==0){
                            output = String.valueOf(-1);
                        } else {
                            output = String.valueOf(deque[0]);
                        }
     
                        break;
     
                    case "back":
                        if(size==0){
                            output = String.valueOf(-1);
                        } else {
                            output = String.valueOf(deque[size-1]);
                        }
     
                        break;     
                
                    default:
                        break;
                }
     
                if(output.length()>0){
                    bw.write(output);
                    bw.newLine();
                    bw.flush();
                }
            }
     
            bw.flush();
            bw.close();
        }
    }
     
    cs

     

     

     

    728x90
    반응형

    댓글

S.B. All Rights Reserved.