ABOUT ME

-

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

     

    이번 포스팅은 백준 10845번 큐입니다.

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

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

     

    10845번: 큐

    첫째 줄에 주어지는 명령의 수 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
    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[] queue = 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":
                        queue[size]=Integer.parseInt(st.nextToken());
                        size++;
     
                        break;
     
                    case "pop":
                        if(size==0){
                            output = String.valueOf(-1);
                        } else {
                            output = String.valueOf(queue[0]);
     
                            int[] queue2 = new int[size-1];
                            int size2=0;
                            for(int j=1; j<size; j++){
                                queue2[size2]=queue[j];
                                size2++;
                            }
     
                            for(int k=0; k<size-1; k++){
                                queue[k] = queue2[k];
                            }
     
                            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(queue[0]);
                        }
     
                        break;
     
                    case "back":
                        if(size==0){
                            output = String.valueOf(-1);
                        } else {
                            output = String.valueOf(queue[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.