diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..c3f502a --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 디폴트 무시된 파일 +/shelf/ +/workspace.xml +# 에디터 기반 HTTP 클라이언트 요청 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..6f29fee --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..e4279f0 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/2024_2_Amicom_Algorithm.iml b/2024_2_Amicom_Algorithm.iml new file mode 100644 index 0000000..eb50ac5 --- /dev/null +++ b/2024_2_Amicom_Algorithm.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git "a/\354\213\240\354\261\204\354\235\200/3\354\243\274\354\260\250/Baekjoon3/Stack.java" "b/\354\213\240\354\261\204\354\235\200/3\354\243\274\354\260\250/Baekjoon3/Stack.java" new file mode 100644 index 0000000..8d7092b --- /dev/null +++ "b/\354\213\240\354\261\204\354\235\200/3\354\243\274\354\260\250/Baekjoon3/Stack.java" @@ -0,0 +1,48 @@ +package Baekjoon3; + +import java.util.List; +import java.util.ArrayList; + +class Stack { + + List stack = new ArrayList<>(); + String stackTop = "-1"; + + public void push(String item) { + stack.add(item); + stackTop = item; + } + + public int pop() { + + System.out.println(stackTop); + + if(stackTop.equals("-1")) { //스텍에 들어있는게 없으므로 아래 과정 진행 X 리턴 + return -1; + } + stack.remove(stack.size() - 1); + if (stack.size() == 0) { + stackTop = "-1"; //스텍 길이가 0이니까 top 초기화 + } else { + stackTop = stack.get(stack.size() - 1); + } + return 0; + } + + public void size() { + System.out.println(stack.size()); + } + + public void empty() { + if (stack.size() == 0) { + System.out.println(1); + } else { + System.out.println(0); + } + } + + public void top() { + System.out.println(stackTop); + } + +} diff --git "a/\354\213\240\354\261\204\354\235\200/3\354\243\274\354\260\250/Baekjoon3/StackController.java" "b/\354\213\240\354\261\204\354\235\200/3\354\243\274\354\260\250/Baekjoon3/StackController.java" new file mode 100644 index 0000000..c339447 --- /dev/null +++ "b/\354\213\240\354\261\204\354\235\200/3\354\243\274\354\260\250/Baekjoon3/StackController.java" @@ -0,0 +1,44 @@ +package Baekjoon3; + +import java.util.Scanner; + +public class StackController { + /* + 정수를 저장하는 스택을 구현한 다음, 입력으로 주어지는 명령을 처리하는 프로그램을 작성하시오. + 명령은 총 다섯 가지이다. + + push X: 정수 X를 스택에 넣는 연산이다. + pop: 스택에서 가장 위에 있는 정수를 빼고, 그 수를 출력한다. 만약 스택에 들어있는 정수가 없는 경우에는 -1을 출력한다. + size: 스택에 들어있는 정수의 개수를 출력한다. + empty: 스택이 비어있으면 1, 아니면 0을 출력한다. + top: 스택의 가장 위에 있는 정수를 출력한다. 만약 스택에 들어있는 정수가 없는 경우에는 -1을 출력한다.*/ + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + int commandCount = Integer.parseInt(sc.nextLine()); + + Stack stack = new Stack(); + + for (int i = 0; i < commandCount; i++) { + String command = sc.nextLine(); + if (command.contains("push")) { + String item = command.split(" ")[1]; //공백을 기준으로 명령어와 입력값 구분 + stack.push(item); + }else if (command.equals("pop")) { + stack.pop(); + }else if(command.equals("size")) { + stack.size(); + } + else if (command.equals("empty")) { + stack.empty(); + }else if (command.equals("top")) { + stack.top(); + }else{ + System.out.println("스택에 해당 명령이 없습니다."); + System.exit(1); + } + } + } + +} diff --git "a/\354\213\240\354\261\204\354\235\200/3\354\243\274\354\260\250/Baekjoon4/Queue.java" "b/\354\213\240\354\261\204\354\235\200/3\354\243\274\354\260\250/Baekjoon4/Queue.java" new file mode 100644 index 0000000..7172082 --- /dev/null +++ "b/\354\213\240\354\261\204\354\235\200/3\354\243\274\354\260\250/Baekjoon4/Queue.java" @@ -0,0 +1,57 @@ +package Baekjoon4; + +import java.util.ArrayList; +import java.util.List; + +class Queue { + List queue = new ArrayList<>(); + String queueFront = "-1"; + String queueBack = "-1"; + + public void push(String item) { + queue.add(item); + if(queueFront.equals("-1")) { //큐에 아무것도 들어오지 않은 경우 + queueFront = item; + } + queueBack = item; + } + + public int pop() { + + System.out.println(queueFront); + + if(queueFront.equals("-1")) { //들어있는게 없으므로 아래 과정 진행 X 리턴 + return -1; + } + + queue.remove(0); + + if (queue.size() == 0) { + queueFront = "-1"; //길이가 0이니까 초기화 + queueBack = "-1"; + } else { + queueFront = queue.get(0); + } + return 0; + } + + public void size() { + System.out.println(queue.size()); + } + + public void empty() { + if (queue.size() == 0) { + System.out.println(1); + } else { + System.out.println(0); + } + } + + public void front() { + System.out.println(queueFront); + } + + public void back() { + System.out.println(queueBack); + } +} diff --git "a/\354\213\240\354\261\204\354\235\200/3\354\243\274\354\260\250/Baekjoon4/QueueController.java" "b/\354\213\240\354\261\204\354\235\200/3\354\243\274\354\260\250/Baekjoon4/QueueController.java" new file mode 100644 index 0000000..329b18d --- /dev/null +++ "b/\354\213\240\354\261\204\354\235\200/3\354\243\274\354\260\250/Baekjoon4/QueueController.java" @@ -0,0 +1,48 @@ +package Baekjoon4; + +import Baekjoon4.Queue; +import java.util.Scanner; +/* +정수를 저장하는 큐를 구현한 다음, 입력으로 주어지는 명령을 처리하는 프로그램을 작성하시오. + +명령은 총 여섯 가지이다. + +push X: 정수 X를 큐에 넣는 연산이다. +pop: 큐에서 가장 앞에 있는 정수를 빼고, 그 수를 출력한다. 만약 큐에 들어있는 정수가 없는 경우에는 -1을 출력한다. +size: 큐에 들어있는 정수의 개수를 출력한다. +empty: 큐가 비어있으면 1, 아니면 0을 출력한다. +front: 큐의 가장 앞에 있는 정수를 출력한다. 만약 큐에 들어있는 정수가 없는 경우에는 -1을 출력한다. +back: 큐의 가장 뒤에 있는 정수를 출력한다. 만약 큐에 들어있는 정수가 없는 경우에는 -1을 출력한다. +*/ + +public class QueueController { + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + int commandCount = Integer.parseInt(sc.nextLine()); + + Queue queue = new Queue(); + + for (int i = 0; i < commandCount; i++) { + String command = sc.nextLine(); + if (command.contains("push")) { + String item = command.split(" ")[1]; //공백을 기준으로 명령어와 입력값 구분 + queue.push(item); + } else if (command.equals("pop")) { + queue.pop(); + } else if (command.equals("size")) { + queue.size(); + } else if (command.equals("empty")) { + queue.empty(); + } else if (command.equals("front")) { + queue.front(); + } else if (command.equals("back")) { + queue.back(); + } else { + System.out.println("스택에 해당 명령이 없습니다."); + System.exit(1); + } + } + } +} diff --git "a/\354\213\240\354\261\204\354\235\200/4\354\243\274\354\260\250/Baekjoon5/DequeController.java" "b/\354\213\240\354\261\204\354\235\200/4\354\243\274\354\260\250/Baekjoon5/DequeController.java" new file mode 100644 index 0000000..2e733a3 --- /dev/null +++ "b/\354\213\240\354\261\204\354\235\200/4\354\243\274\354\260\250/Baekjoon5/DequeController.java" @@ -0,0 +1,76 @@ +package Baekjoon5; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayDeque; +import java.util.Deque; + +public class DequeController { + static Deque arrayDeque = new ArrayDeque<>(); + + public static void main(String[] args) throws IOException { + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int repeatCount = Integer.parseInt(br.readLine()); + + for (int i = 0; i < repeatCount; i++) { + + String input = br.readLine(); + + if(input.contains("push_front")){ + arrayDeque.addFirst(input.split(" ")[1]); + }else if(input.contains("push_back")){ + arrayDeque.addLast(input.split(" ")[1]); + }else if(input.contains("pop_front")){ + try{ + System.out.println(arrayDeque.pop()); + } catch (Exception e){ + System.out.println(-1); + } + }else if(input.contains("pop_back")){ + try{ + System.out.println(arrayDeque.removeLast()); + }catch (Exception e){ + System.out.println(-1); + } + }else if(input.contains("size")){ + System.out.println(arrayDeque.size()); + }else if(input.contains("empty")){ + printEmpty(arrayDeque.isEmpty()); + }else if(input.contains("front")){ + printFront(); + }else if(input.contains("back")){ + printBack(); + }else{ + System.out.println("정상적인 입력이 아닙니다."); + System.exit(-1); + } + + } + } + + private static void printEmpty(boolean isEmpty){ + if(isEmpty){ + System.out.println(1); + }else{ + System.out.println(0); + } + } + + private static void printFront() { + if (arrayDeque.peekFirst() == null) { + System.out.println(-1); + } else { + System.out.println(arrayDeque.peekFirst()); + } + } + + private static void printBack() { + if (arrayDeque.peekLast() == null) { + System.out.println(-1); + } else { + System.out.println(arrayDeque.peekLast()); + } + } +} diff --git "a/\354\213\240\354\261\204\354\235\200/4\354\243\274\354\260\250/Baekjoon6/BalancedBrackets.java" "b/\354\213\240\354\261\204\354\235\200/4\354\243\274\354\260\250/Baekjoon6/BalancedBrackets.java" new file mode 100644 index 0000000..50406f7 --- /dev/null +++ "b/\354\213\240\354\261\204\354\235\200/4\354\243\274\354\260\250/Baekjoon6/BalancedBrackets.java" @@ -0,0 +1,47 @@ +package Baekjoon6; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Stack; + +public class BalancedBrackets { + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + while (true) { + String userInput = br.readLine(); + if (userInput.equals(".")) { + break; + } + + if(isBalanced(userInput)) { + System.out.println("yes"); + }else{ + System.out.println("no"); + } + } + } + + private static boolean isBalanced(String input) { + Stack stack = new Stack<>(); + + for (int i = 0; i < input.length(); i++) { + char ch = input.charAt(i); + + if (ch == '(' || ch == '[') { + stack.push(ch); + } + + else if (ch == ')' && (stack.isEmpty() || stack.pop() != '(')) { + return false; + } else if (ch == ']' && (stack.isEmpty() || stack.pop() != '[')) { + return false; + } + } + + + return stack.isEmpty(); + } +} \ No newline at end of file