-
Notifications
You must be signed in to change notification settings - Fork 1
/
1935
51 lines (45 loc) · 1.24 KB
/
1935
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Stack<Double> stack = new Stack<>();
int N = Integer.parseInt(br.readLine());
String arr = br.readLine();
Double[] data = new Double[N];
for(int i=0;i<N;i++) {
data[i] = Double.parseDouble(br.readLine());
}
for(int i=0;i<arr.length();i++) {
if(arr.charAt(i)>='A'&&arr.charAt(i)<='Z') {
stack.push(data[arr.charAt(i)-'A']);
}
else {
if(arr.charAt(i)=='*') {
Double first = stack.pop();
Double second = stack.pop();
stack.push(second*first);
}
else if(arr.charAt(i)=='/') {
Double first = stack.pop();
Double second = stack.pop();
stack.push(second/first);
}
else if(arr.charAt(i)=='+') {
Double first = stack.pop();
Double second = stack.pop();
stack.push(second+first);
}
else {
Double first = stack.pop();
Double second = stack.pop();
stack.push(second-first);
}
}
}
System.out.printf("%.2f", stack.peek());
}
}