Notice
Recent Posts
Recent Comments
Link
«   2025/04   »
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
Tags
more
Archives
Today
Total
관리 메뉴

걸음마부터 달리기

SWEA 4874 스택과 후위연 본문

카테고리 없음

SWEA 4874 스택과 후위연

성추 2025. 4. 10. 14:08

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=2&contestProbId=AWTQc1MKQiIDFAVT&categoryId=AWTQc1MKQiIDFAVT&categoryType=CODE&problemTitle=&orderBy=SUBMIT_COUNT&selectCodeLang=ALL&select-1=2&pageSize=10&pageIndex=1

 

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;
        }
    }
}

파이썬만 제출 가능한걸 나중에 봐서 아쉬운대로 기록