μ΅œλŒ€ 1 λΆ„ μ†Œμš”

πŸ“ [S3_1935] ν›„μœ„ ν‘œκΈ°μ‹2

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 NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		
		int N = Integer.parseInt(br.readLine());
		
		String str = br.readLine();
		
		// 숫자 λ°°μ—΄
		int[] arr = new int[N];
		for(int i=0; i<N; i++) {
			arr[i] = Integer.parseInt(br.readLine());
		}
		
		Stack<Double> q = new Stack<>();
		
		for(int i=0; i<str.length(); i++) {
			char x = str.charAt(i);
			if(str.charAt(i) - 65 >= 0 && str.charAt(i) - 90 <= 0) {
				q.add(arr[x-65]*1.0);
			}
			else {			
				double a = q.pop();
				double b = q.pop();
				double c = 0.0;
			switch(x) {
			case '*':
				c = b * a;
				break;
			case '/':
				c = b / a;
				break;
			case '+':
				c = b + a;
				break;
			case '-':
				c = b - a;
				break;
			}
			q.add(c);
			}

		}
		double x = q.pop();
		System.out.printf("%.2f", x);
		
	} 


}

πŸ€” λ‚˜μ˜ 생각

STACK을 톡해 문제λ₯Ό ν‘ΈλŠ” 것이닀.
ν›„μœ„ ν‘œκΈ°μ‹κ³Ό STACK의 ꡬ쑰만 μ΄ν•΄ν•˜κ³  μžˆλ‹€λ©΄ κ°„λ‹¨ν–ˆμ„ λ¬Έμ œλ‹€.