매일 Algorithm

신재원·2023년 6월 30일
0

Algorithm

목록 보기
156/243

백준 1935번

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br
                = new BufferedReader(new InputStreamReader(System.in));
        int size = Integer.parseInt(br.readLine());
        int[] numArr = new int[size];
        String input = br.readLine();

        for (int i = 0; i < size; i++) {
            numArr[i] = Integer.parseInt(br.readLine());
        }

        Stack<Double> stack = new Stack<>();
        int length = input.length();

        for (int i = 0; i < length; i++) {
            char temp = input.charAt(i);
            if (temp >= 'A' && temp <= 'Z') {
                // 배열 값을 스택에 할당
                double doubleNum = numArr[temp - 'A'];
                stack.push(doubleNum);
            } else {
                // 연산자인 경우
                double pop1 = stack.pop();
                double pop2 = stack.pop();
                double result = 0.0;
                // 스택의 특징 FILO 이유로 pop2 부터 연산을 합니다.
                switch (temp) {
                    case '+':
                        result = pop2 + pop1;
                        break;

                    case '-':
                        result = pop2 - pop1;
                        break;
                    case '*':
                        result = pop2 * pop1;
                        break;
                    case '/':
                        result = pop2 / pop1;
                        break;
                }
                stack.push(result);
            }
        }

        System.out.printf("%.2f", stack.pop());
    }
}

백준 1620번

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class problem495 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int size = in.nextInt();
        int findNum = in.nextInt();
        Map<String, String> map = new HashMap<>();


        for (int i = 0; i < size; i++) {
            String poket = in.next();
            String count = Integer.toString(i + 1);

            map.put(poket, count);
            map.put(count, poket);
        }

        StringBuilder output = new StringBuilder();

        for (int i = 0; i < findNum; i++) {
            String find = in.next();
            output.append(map.get(find)).append("\n");
        }

        System.out.print(output.toString());
    }
}

백준 11279번

import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Scanner;

public class problem496 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int size = in.nextInt();
        StringBuilder output = new StringBuilder();
        // 우선순위 큐는 기본적으로 오름차순 정렬이다.
        PriorityQueue<Integer> pq 
        		= new PriorityQueue<>(Collections.reverseOrder());
        for (int i = 0; i < size; i++) {
            int inputNum = in.nextInt();

            // 입력값이 0 인 경우
            if (inputNum == 0) {
                if (pq.isEmpty()) {
                    output.append(0).append("\n");
                }
                // 큐가 Empty가 아닌경우
                else {
                    output.append(pq.poll()).append("\n");
                }
            }
            pq.offer(inputNum);

        }
        System.out.print(output.toString());
    }
}

0개의 댓글