728x90
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 N = Integer.parseInt(br.readLine());
int[] arr = new int[N];
Stack<Double>s = new Stack<Double>();
String str = br.readLine();
for(int i = 0; i< N; i++) {
arr[i] = Integer.parseInt(br.readLine());
}
for(int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if(c >= 'A' && c <= 'Z') {
s.push((double)arr[c-'A']);
}
else {
double num2 = s.pop();
double num1 = s.pop();
switch(c) {
case '+':
s.push(num1 + num2);
break;
case '-':
s.push(num1 - num2);
break;
case '*':
s.push(num1 * num2);
break;
case '/':
s.push(num1 / num2);
break;
}
}
}
System.out.printf("%.2f",s.pop());
}
}
다른사람들의 답안을 참고했다..
Stack을 double형으로 지정해줘야하는 것
문자로 입력을 받아서 그 문자를 숫자형으로 스택에 넣어주는 방법
후위표기법을 까먹었다 고새.. 나중에 pop한 숫자가 앞으로 가서 계산해줘야한다.
728x90
'CS > Algorithm' 카테고리의 다른 글
[백준 : 4889] 안정적인 문자열 - JAVA (0) | 2021.04.05 |
---|---|
[백준:3986번] 좋은 단어 - JAVA (0) | 2021.04.04 |
[ 백준 10799 ] : 쇠막대기 - JAVA (0) | 2021.04.04 |
ArrayList와 LinkedList의 차이점 [ Java ] (0) | 2021.04.04 |
[백준 : 10773] 제로 - JAVA (0) | 2021.04.04 |