-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path210224_1.txt
50 lines (44 loc) · 1.14 KB
/
210224_1.txt
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
//15회 E-PPER 6번 후위표기법
import java.io.*;
import java.util.*;
public class Main {
public static int solution(int m, String input) {
//이 아래 필요한 변수 및 필요한 코드를 작성하세요
int answer = 0;
int a =0;
int b =0;
Stack<Integer> stack = new Stack<Integer>();
for(int i=0; i<input.length();i++){
if(input.charAt(i)<='9'&&input.charAt(i)>='1'){
stack.push(input.charAt(i)-'0');
}else if(input.charAt(i)=='+'){
a=stack.pop();
b=stack.pop();
stack.push(a+b);
}else if(input.charAt(i)=='-'){
a=stack.pop();
b=stack.pop();
stack.push(b-a);
}else if(input.charAt(i)=='*'){
a=stack.pop();
b=stack.pop();
stack.push(a*b);
}else if(input.charAt(i)=='/'){
a=stack.pop();
b=stack.pop();
stack.push(b/a);
}
}
answer = stack.peek();
return answer;
}
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
int m = input.nextInt();
input.nextLine();//엔터 제거 위해 임의로 추가
String str = input.nextLine();
int result;
result=solution(m, str);
System.out.println(result);
}
}