Search
Duplicate

Stack, Queue

상태
완료
담당자

1) 올바른 괄호

설명
package com.company.stackandqueue; import java.util.Scanner; import java.util.Stack; public class CorrectParenthesis { public static void main (String [] args) { CorrectParenthesis correctParenthesis = new CorrectParenthesis(); Scanner in = new Scanner(System.in); String str = in.next(); correctParenthesis.solution(str); } boolean solution(String str) { boolean answer = true; Stack<Character> stack = new Stack<>(); char [] ar = str.toCharArray(); for (char c : ar) { if (c == '(') { stack.push(c); } else { if (!stack.isEmpty()) { stack.pop(); } else { answer = false; } } } if(!stack.isEmpty()) answer = false; System.out.println(answer); return answer; } }
Java
복사

2) 괄호 문자 제거

설명
package com.company.stackandqueue; import java.util.Scanner; import java.util.Stack; public class removeParenthesis { public static void main (String [] args) { removeParenthesis removeParenthesis = new removeParenthesis(); Scanner in = new Scanner(System.in); String str = in.next(); removeParenthesis.solution(str); } void solution(String str) { String answer = ""; Stack<Character> stack = new Stack<>(); char [] ar = str.toCharArray(); for (char c : ar) { if (c == ')') { while (stack.pop() != '('); } else { stack.push(c); } } for (Character character : stack) { answer += character; } System.out.println(answer); } }
Java
복사

3) 크레인 인형뽑기 (카카오)

설명
package com.company.stackandqueue; import java.util.Scanner; import java.util.Stack; public class KakaoBalloonPop { public static void main (String [] args) { KakaoBalloonPop kakaoBalloonPop = new KakaoBalloonPop(); Scanner in = new Scanner(System.in); int boardCount = in.nextInt(); int [][] board = new int[boardCount][boardCount]; for(int i=0; i<boardCount; i++) { for(int j=0; j<boardCount; j++) { board[i][j] = in.nextInt(); } } int moveCount = in.nextInt(); int [] moves = new int[moveCount]; for(int i=0; i<moveCount; i++) { moves[i] = in.nextInt(); } kakaoBalloonPop.solution(boardCount, board, moveCount, moves); } void solution(int boardCount, int [][] board, int moveCount, int [] moves) { int answer = 0; Stack<Integer> stack = new Stack<>(); for(int i=0; i<moveCount; i++) { for(int j=0; j<boardCount; j++) { if (board[j][moves[i]-1] != 0) { int temp = board[j][moves[i]-1]; board[j][moves[i]-1] = 0; if (!stack.isEmpty() && temp == stack.peek()) { answer+=2; stack.pop(); } else { stack.push(temp); } break; } } } System.out.println(answer); } }
Java
복사

4) 후위식 연산

설명
package com.company.stackandqueue; import java.util.Scanner; import java.util.Stack; public class Postfix { public static void main (String [] args) { Postfix postfix = new Postfix(); Scanner in = new Scanner(System.in); String str = in.next(); postfix.solution(str); } void solution(String str) { char [] arr = str.toCharArray(); Stack<Integer> stack = new Stack<>(); for (char c : arr) { if ( Character.isDigit(c)) { stack.push((c - '0')); //숫자 } else { //특수문자 int rt = stack.pop(); int lt = stack.pop(); int calcResult = 0; switch (c) { case '+' : calcResult = lt+rt; break; case '-' : calcResult = lt-rt; break; case '*' : calcResult = lt*rt; break; case '/' : calcResult = lt/rt; break; } stack.push(calcResult); } } System.out.println(stack.get(0)); } }
Java
복사

5) 쇠막대기

설명
package com.company.stackandqueue; import java.util.Scanner; import java.util.Stack; public class IronRod { public static void main (String [] args) { IronRod ironRod = new IronRod(); Scanner in = new Scanner(System.in); String str = in.next(); ironRod.solution(str); } void solution(String str) { Stack<Character> stack = new Stack<>(); int answer = 0; char [] arr = str.toCharArray(); for (int i=0; i<arr.length; i++) { if (arr[i] == '(') { stack.push(arr[i]); } else { stack.pop(); if (arr[i-1] == '(') { answer += stack.size(); } else { answer += 1; } } } System.out.println(answer); } }
Java
복사

6) 공주 구하기

설명
package com.company.stackandqueue; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class SaveThePrincess { public static void main (String [] args) { SaveThePrincess saveThePrincess = new SaveThePrincess(); Scanner in = new Scanner(System.in); int n = in.nextInt(); int k = in.nextInt(); saveThePrincess.solution(n, k); } void solution(int n, int k) { Queue<Integer> queue = new LinkedList<>(); for (int i=1; i<=n; i++) { queue.offer(i); } while(!queue.isEmpty()) { for (int i=0; i<k; i++) { queue.offer(queue.poll()); } queue.poll(); if (queue.size() == 1) { System.out.println(queue.poll()); } } } }
Java
복사

7) 교육과정설계

설명
package com.company.stackandqueue; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class EducationPlan { public static void main (String [] args) { EducationPlan educationPlan = new EducationPlan(); Scanner in = new Scanner(System.in); String need = in.next(); String plan = in.next(); educationPlan.solution(need, plan); } void solution(String need, String plan) { boolean result = true; Queue<Character> queue = new LinkedList<>(); for (Character c : need.toCharArray()) { queue.offer(c); } for (Character c : plan.toCharArray()) { if (queue.contains(c)) { if (queue.poll() != c) { result = false; } } } if(queue.size() > 0) { result = false; } System.out.println(result); } }
Java
복사

8) 응급실

설명
class Patient { private int seq; private int risk; public Patient(int seq, int risk) { this.seq = seq; this.risk = risk; } public int getSeq() { return seq; } public int getRisk() { return risk; } @Override public String toString() { return "Patient{" + "seq=" + seq + ", risk=" + risk + '}'; } } public class EmergencyRoom { public static void main (String [] args) { EmergencyRoom educationPlan = new EmergencyRoom(); Scanner in = new Scanner(System.in); int humanCount = in.nextInt(); int number = in.nextInt(); int [] risks = new int[humanCount]; for (int i=0; i<humanCount; i++) { risks[i] = in.nextInt(); } educationPlan.solution(humanCount, number, risks); } void solution(int humanCount, int number, int [] risks) { int answer = 0; Queue<Patient> queue = new LinkedList<>(); for (int i=0; i<humanCount; i++) { queue.offer(new Patient(i, risks[i])); } while(!queue.isEmpty()) { Patient temp = queue.poll(); for (Patient patient : queue) { if (temp.getRisk() < patient.getRisk()) { queue.offer(temp); temp = null; break; } } //진료를 받을 수 있음 if (temp != null) { answer++; if (temp.getSeq() == number) System.out.println(answer); } } } }
Java
복사