[코테5_4] 후위식 연산

byeol·2022년 12월 12일
0

코딩테스트

목록 보기
32/42

아스키 외우자

✔️ 나의 답-> 정답

import java.util.*;


public class Main {
 public static int solution(String input){
	 int answer=0;
	 Stack<Integer>st = new Stack<>();
	 for(char x : input.toCharArray()) {
		 if(x>=48 && x<=57) 
			 st.push((int)(x-'0'));
		 else {
			 int result;
			 int a=st.pop();
			 int b=st.pop();
			 switch(x) {
			 case '+':
				 result=b+a;
				 st.push(result);
				 break;
			 case '-':
				 result=b-a;
				 st.push(result);
				 break;
			 case '*':
				 result=b*a;
				 st.push(result);
				 break;
			 case '/':
				 result=b/a;
				 st.push(result);
				 break;
			 }
		 }
	 }
	 answer = st.pop();
	 
	 
	 
	 return answer;

 }
 public static void main(String[] args){
  Scanner kb = new Scanner(System.in);
  String input = kb.next();
  System.out.println(solution(input));
 }
}
profile
꾸준하게 Ready, Set, Go!

0개의 댓글