-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1935(Stack).java
More file actions
50 lines (45 loc) · 1.66 KB
/
1935(Stack).java
File metadata and controls
50 lines (45 loc) · 1.66 KB
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
import java.io.*;
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));
Double arr[] = new Double[100];
int N = Integer.parseInt(br.readLine());
String s = br.readLine();
int a = 65;
for(int i = 0; i < N; i++){
arr[a] = Double.parseDouble(br.readLine());
a++;
}
Stack<Double>stack = new Stack<>();
int length = s.length();
for(int i = 0; i < length; i++){
if(s.charAt(i) >= 65 && s.charAt(i) <= 90){
stack.push(arr[s.charAt(i)]);
}else{
if(s.charAt(i) == '+'){
double second = stack.pop();
double first = stack.pop();
stack.push(second+first);
}else if(s.charAt(i) == '*'){
double second = stack.pop();
double first = stack.pop();
stack.push(second*first);
}else if(s.charAt(i) == '-'){
double second = stack.pop();
double first = stack.pop();
stack.push(first - second);
}else if(s.charAt(i) == '/'){
double second = stack.pop();
double first = stack.pop();
stack.push(first/second);
}
}
}
double answer = stack.pop();
System.out.println(String.format("%.2f", answer));
}
}
//stack에 뒤에서 부터 차례로 넣는다
//