-
백준 10866 덱 | 자바 java | BufferedReader BufferedWriter[백준 알고리즘]/[자바 Java] 2024. 1. 18. 17:35728x90반응형
이번 포스팅은 백준 10866번 덱입니다.
아래 url를 클릭하시면 백준 사이트에서 문제를 볼 수 있습니다.
https://www.acmicpc.net/problem/10866
기본 알고리즘
- 구현
- 자료구조
- 덱
반응형전체 코드
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128import 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반응형'[백준 알고리즘] > [자바 Java]' 카테고리의 다른 글
백준 2751 수 정렬하기 2 | 자바 java | Arrays.sort BufferedReader BufferedWriter (0) 2024.01.22 백준 1181 단어 정렬 | 자바 java | Arrays.sort Comparator BufferedReader BufferedWriter (0) 2024.01.22 백준 10845 큐 | 자바 java | BufferedReader BufferedWriter (0) 2024.01.18 백준 10828 스택 | 자바 java | BufferedReader BufferedWriter (0) 2024.01.12 백준 2798 블랙잭 | 자바 java (0) 2024.01.11