걸음마부터 달리기
SWEA 4874 스택과 후위연 본문
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
import java.util.*;
import java.lang.*;
import java.io.*;
// The main method must be in a class named "Main".
class main {
static int T;
static int[][] map;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
T = Integer.parseInt(br.readLine());
for (int i = 0; i < T; i++) {
Stack<Integer> stack = new Stack<>();
String[] s = br.readLine().split(" ");
for(int j = 0; j < s.length; j++) {
if(s[j].equals("+") || s[j].equals("-") || s[j].equals("*") || s[j].equals("/")){
if(stack.size()<2){
System.out.printf("#%d %s\n",i+1,"error");
break;
}
else{
Integer i1 = stack.pop();
Integer i2 = stack.pop(); //i2 연산 i1
int cal = cal(s[j], i2, i1);
stack.push(cal);
}
}
else if(!s[j].equals(".")){
stack.push(Integer.parseInt(s[j]));
}
else{
int ans = stack.pop();
System.out.printf("#%d %d\n",i+1,ans);
}
}
}
}
public static int cal(String s,int i2 , int i1){
if(s.equals("+")) {
return i1+i2;
}
else if(s.equals("-")) {
return i2-i1;
}
else if(s.equals("*")) {
return i1*i2;
}
else{
return i2/i1;
}
}
}
파이썬만 제출 가능한걸 나중에 봐서 아쉬운대로 기록