BOJ_S3_1935
π [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μ κ΅¬μ‘°λ§ μ΄ν΄νκ³ μλ€λ©΄ κ°λ¨νμ λ¬Έμ λ€.